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:
Anycan represent an instance of any type at all, including function types.AnyObjectcan 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 (Int, Double, Float, String, Array, 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 Int, Double, Floatliterals 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
swift 类型系统 Self self Type的更多相关文章
- 随手记Swift基础和Optional Type(问号?和感叹号!)
距离Apple推出Swift已经有几天了,网上也时不时出现"急招Swift程序猿,要求有一天工作经验"的帖子. 看到Swift,除了苹果放的另外一门语言的链接(http://swi ...
- 寒哥带你深入了解下Swift中的Value Type
http://www.cocoachina.com/swift/20150923/13539.html 关于开发到底使用ValueType 值类型还是Reference Type 引用类型,关于这个, ...
- swift 定义类方法(type methed)
swift 中声明结构体或者枚举的类型方法,需要在func前加上关键字 ststic ,但是如果要定义一个类的类方法时,需要用关键字 class class SomeClass { class ...
- Swift:Minimizing Annotation with Type Inference
许多程序猿更喜欢比如Python和Javascript这样的动态语言,因为这些语言并不要求程序猿为每个变量声明和管理它们的类型. 在大多数动态类型的语言里,变量可以是任何类型,而类型声明是可选的或者根 ...
- swift:Optional Type 、Swift和Objective-C混编的讲解
❤️❤️❤️swift中的Optional Type的?和!含义:其实就是一个装包和拆包的过程 optional的含义: Optional事实上是一个枚举类型,Optional包含None和Some两 ...
- iOS开发系列--Swift进阶
概述 上一篇文章<iOS开发系列--Swift语言>中对Swift的语法特点以及它和C.ObjC等其他语言的用法区别进行了介绍.当然,这只是Swift的入门基础,但是仅仅了解这些对于使用S ...
- iOS8.3发布了Swift 1.2带来哪些新变化
苹果前几日在面向开发者推送iOS 8.3 Beta的同时,还发布了版本号为6D520o的Xcode 6.3 Beta,其中便包含了iOS 8.3 Beta和OS X v10.10 SDK,并进一步提升 ...
- Swift数据类型简介(二)
整数 整数就是没有小数部分的数字,比如42和-23.整数可以是有符号(正.负.零)或者无符号(正.零). Swift 提供了8,16,32和64位的有符号和无符号整数类型.这些整数类型和 C 语言的命 ...
- swift中文文档- 类型转换
未翻译完 待续(英语烂,求斧正) Type Casting 类型转换 Type casting is a way to check the type of an instance, and/or to ...
随机推荐
- 【codeforces 792D】Paths in a Complete Binary Tree
[题目链接]:http://codeforces.com/contest/792/problem/D [题意] 给你一棵满二叉树; 给你初始节点; 给你若干个往上走,左走,右走操作; 让你输出一系列操 ...
- 【codeforces 758D】Ability To Convert
[题目链接]:http://codeforces.com/contest/758/problem/D [题意] 给你一个n进制的数k; 问你它可能的最小的十进制数是多少; [题解] 从右往左; 获取数 ...
- [BZOJ1031][JSOI2007]字符加密Cipher(后缀数组)
传送门 算是个模板. 题目说循环,那就再复制一串拼接上. 然后求后缀数组,再搞就可以. 虽然是求后缀,会在后面多一些字符串,然而题目中说的是循环一圈,但是没有影响. ——代码 #include < ...
- lead 函数和 lag函数
这两个函数的作用只能通过例子来解释,否则说不明白. 首先创建一个表 SQL> create table test (id number, name varchar2(8), val number ...
- Servlet仿CSDN动态验证码的生成-带数字和字母
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 一.实现的思路: (1)首先,须要创建一个Servlet.该Servlet通过字节型响应给cl ...
- Oracle学习(一):基本操作和基本查询语句
文中以"--"开头的语句为凝视,即为绿色部分 1.知识点:能够对比以下的录屏进行阅读 SQL> --录屏工具spool,開始录制,并指定保存路径为c:\基本查询.txt SQ ...
- BeanUtils使用案例
1.BeanUtils框架/工具(APACHE开源组织开发) (1)BeanUtils框架可以完毕内省的一切功能.并且优化 (2)BeanUtils框架可以对String<-> ...
- 清橙A1206.小Z的袜子 && CF 86D(莫队两题)
清橙A1206.小Z的袜子 && CF 86D(莫队两题) 在网上看了一些别人写的关于莫队算法的介绍,我认为,莫队与其说是一种算法,不如说是一种思想,他通过先分块再排序来优化离线查询问 ...
- DNS隐蔽通道 是可以通过dig 子域名来追踪其真实IP的
比如a.friendskaka.com 是我的外发子域名,那么可以按照下面两个命令来追踪IP: bonelee@bonelee-VirtualBox:~/桌面$ dig auth.a.friendsk ...
- codeforces 915D Almost Acyclic Graph 拓扑排序
大意:给出一个有向图,问能否在只去掉一条边的情况下破掉所有的环 解析:最直接的是枚举每个边,将其禁用,然后在图中找环,如果可以就YES,都不行就NO 复杂度O(N*M)看起来不超时 但是实现了以后发现 ...