• 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

  • 相关阅读:
    [手把手]VMware 16 pro 装 Windows11专业版并激活
    [HTML] 做个空壳网页练手(菜鸡的自我信息完善
    从零玩HTML的一天
    [总结]C++ 之 向量vector
    [递归专题打卡]2021 6.30-7.2
    初学Socket笔记
    对java是编译型语言还是解释型语言的讨论
    PHP CURL POST 请求设置 Content-Type (指定Content-Type)
    webpack 报错 [webpack-cli] Unable to load '@webpack-cli/serve' command
    Vue cli 创建项目模板 / npm run build 打包后资源引用问题
  • 原文地址:https://www.cnblogs.com/feng9exe/p/9182431.html
Copyright © 2020-2023  润新知