swift 类型系统 Self self Type

namedClass:静态类型;与类型实现直接关联;可以用于初始化、类型检查等。

namedClass.self:@thick,脱敏(脱关)类型;动态类型;可以作为元类型的实例;可以作为类型参量进行传递;

可以用于继承体系;

 

使用脱敏类型进行初始化时,需要与具体类型进行绑定。

 

func  forClass() -> Swift.AnyClass?

    {

        let nameKey = "CFBundleName"

        let appName = Bundle.main.object(forInfoDictionaryKey: nameKey) as? String   //这里也是坑,请不要翻译oc的代码,而是去NSBundle类里面看它的api

        //return appName!

        

        let className = appName! + "." + self

        

        return NSClassFromString(className)

    }

let model = vcName.forClass() as! UIViewController.Type;

 

namedClass.Type:元类型;用于脱敏类型声明;脱敏类型类型检查。

 

动态类型:编译时不确定类型指定。

 

Self:动态时的具体类型。

Self:In that context, Self refers to the eventual type that conforms to the protocol.

代表具体的实际类型;动态类型。

https://docs.swift.org/swift-book/ReferenceManual/Declarations.html

 

"Self" is a placeholder used in two different cases:

 

1. In a protocol, it refers to the type that conforms to the protocol in any particular use. In Equatable, for example, it's used to require that the two values being compared are of the same type. It's something like a generic type parameter that you don't have to put between the <…> because it's deduced from the context of its use.

 

2. In a class/static method, it can be used as the return type, to indicate that the return type is the type of the class to which the method was sent, rather than the class in which the method is declared. It's similar to 'instancetype' in Obj-C.

 

AnyObject:

an untyped object

 

/// The flexible behavior of the `AnyObject` protocol is similar to

/// Objective-C's `id` type. For this reason, imported Objective-C types

/// frequently use `AnyObject` as the type for properties, method parameters,

/// and return values.

///

/// Casting AnyObject Instances to a Known Type

/// ===========================================

///

/// Objects with a concrete type of `AnyObject` maintain a specific dynamic

/// type and can be cast to that type using one of the type-cast operators

/// (`as`, `as?`, or `as!`).

 

Swift provides two special types for working with nonspecific types:

  • Any can represent an instance of any type at all, including function types.
  • AnyObject can represent an instance of any class type.

 

Whether you use Any or AnyObject depends on your intended use:

 

If your dictionary will be used only within Swift code, then you should use Any because your types (IntDoubleFloatStringArray, and Dictionary) are not objects.

 

If you will be passing your dictionary to Objective-C routines that expect an NSDictionary, then you should use AnyObject.

 

When you import Foundation or import UIKit or import Cocoa, it is possible to declare your array as [String: AnyObject], but in this case Swift is treating your IntDoubleFloatliterals as NSNumber, your Strings as NSString, your Arrays as NSArray, and your dictionaries as NSDictionary, all of which are objects. A dictionary using AnyObject as the value type is convertible to NSDictionary, but one using Any is not.

 

https://stackoverflow.com/questions/25809168/anyobject-and-any-in-swift

 

转载于:https://www.cnblogs.com/feng9exe/p/9182431.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值