zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    层的问题,
    创建一个函数,返回元素个数为n的int型数组中的最小值
    c语言中编写函数求五个学生中的最高分
    c语言中没有形参的函数(例题:逆向输出正整数)
    创建一个函数,对元素个数为n的int型数组进行倒序排列。
    c语言 函数中数组的传递, 形参和实参。
    c语言中函数的传递和const类型的修饰符
    c语言中的文件作用域、函数原型声明、定义声明和非定义声明
    c语言中文件包含指令和头文件
    c语言中数组元素的线性查找
  • 原文地址:https://www.cnblogs.com/feng9exe/p/9182431.html
Copyright © 2011-2022 走看看