本文是学习《The Swift Programming Language》整理的相关随笔,基本的语法不作介绍,主要介绍Swift中的一些特性或者与OC差异点。
系列文章:
- Swift4 基础部分:The Basics
- Swift4 基础部分:Basic Operators
- Swift4 基础部分:Strings and Characters
- Swift4 基础部分:Collection Types
- Swift4 基础部分:Control Flow
- Swift4 基础部分:Functions
- Swift4 基础部分:Closures
- Swift4 基础部分: Enumerations
- Swift4 基础部分: Classes and Structures
- Swift4 基础部分: Properties
- Swift4 基础部分: Methods
- Swift4 基础部分: Subscripts
- Swift4 基础部分: Inheritance
- Swift4 基础部分: Initialization
- Swift4 基础部分: Deinitialization
- Swift4 基础部分: Automatic Reference Counting(自动引用计数)
- Swift4 基础部分: Optional Chaining(可选链)
- Swift4 基础部分: Error Handling(错误处理)
Type casting is a way to check the type of an instance, or to treat
that instance as a different superclass or subclass from somewhere
else in its own class hierarchy.
- 类型转换可以用来检测实例的类型,或者将实例看成父类或子类的实例。
为类型转化定义一个类的层级(Defining a Class Hierarchy for Type Casting)
例子:
class MediaItem {
var name:String;
init(name:String){
self.name = name;
}
}
class Movie:MediaItem {
var director:String;
init(name:String,director:String){
self.director = director;
super.init(name: name);
}
}
class Song:MediaItem {
var artist:String;
init(name:String,artist:String){
self.artist = artist;
super.init(name: name);
}
}
let library = [
Movie(name: "Casablanca", director: "Michael Curtiz"),
Song(name: "Blue Suede Shoes", artist: "Elvis Presley"),
Movie(name: "Citizen Kane", director: "Orson Welles"),
Song(name: "The One And Only", artist: "Chesney Hawkes"),
Song(name: "Never Gonna Give You Up", artist: "Rick Astley")
];
检查类型(Checking Type)
Use the type check operator (is) to check whether an instance is of a
certain subclass type.
- 利用检查操作符
is
去检查实例是否是一个特定的子类类型。
例子:
var movieCount = 0;
var songCount = 0;
for item in library {
if item is Movie {
movieCount += 1;
} else if item is Song {
songCount += 1;
}
}
print("Media library contains \(movieCount) movies and \(songCount) songs");
执行结果:
Media library contains 2 movies and 3 songs
向下转换(Downcasting)
A constant or variable of a certain class type may actually refer to
an instance of a subclass behind the scenes. Where you believe this is
the case, you can try to downcast to the subclass type with a type
cast operator (as? or as!).
- 当一个常量或变量实际上是一个子类的实例时,可以使用
as? or as
向下转化。
例子:
for item in library {
if let movie = item as? Movie {
print("Movie: \(movie.name), dir. \(movie.director)");
}else if let song = item as? Song {
print("Movie: \(song.name), by \(song.artist)");
}
}
执行结果:
Media library contains 2 movies and 3 songs
Movie: Casablanca, dir. Michael Curtiz
Movie: Blue Suede Shoes, by Elvis Presley
Movie: Citizen Kane, dir. Orson Welles
Movie: The One And Only, by Chesney Hawkes
Movie: Never Gonna Give You Up, by Rick Astley
Any
与AnyObject
的类型转换(Type Casting for Any and AnyObject)
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.
Any
可以表示任何类型的实例包括函数类型。Anyobject
可以表示任何类类型的实例。
例子:
var things = [Any]();
things.append(0);
things.append(0.1);
things.append((3.0,5.0));
things.append("hello");
for thing in things {
switch thing {
case 0 as Int:
print("zero as an Int");
case 0 as Double:
print("zero as a Double");
case let someInt as Int:
print("an integer value of \(someInt)")
case let someDouble as Double where someDouble > 0:
print("a positive double value of \(someDouble)");
case let someString as String:
print("a string value of \"\(someString)\"")
case let (x, y) as (Double, Double):
print("an (x, y) point at \(x), \(y)")
default:
print("something else");
}
}
执行结果:
zero as an Int
a positive double value of 0.1
an (x, y) point at 3.0, 5.0
a string value of "hello"
这里简单扩展一下NSObject
,AnyObject
,Any
的区别。
例子
class Example{}
class ObjectExample:NSObject{}
var example:Example = Example();
var objectExample:ObjectExample = ObjectExample();
print(example is NSObject);
print(example is AnyObject);
print(objectExample is NSObject);
print(objectExample is AnyObject);
执行结果
false
true
true
true
- 1.每一个
NSObject
对象都是AnyObject
,单并非每一个AnyObject
都是NSObject
。- 2.
NSObject
Swift中需要显示继承。