Intro
Note of More Swift and Foundation Frameworks, not detail described.
Lesson4的要点记录,有些没有展开,侧重于“点”。看完后觉得需要注意的地方:基本数据类型如Optional/Array/Dictionary等的特点;class与struct的异同;class中的属性和方法;常见数据类型的常用方法(Array/String有很多新增的方法,功能比对应的ObjC强大)
Optional
an optional is an enum
enum Optional
{
case None
case Some(T)
}
! 相当于一个switch
简单的说,就是语法糖
Array
declare
immutable
enumerating
Dictionary
like above
enumerating using tuple
Range
it’s new
Array’s range :Range<Int>
String’s : Range<String.Index>
… / ..< (close/open)
enumerating
for i in [start…end]{}
Other class
NSObject
advanced features require subclass from NSObject
NSNumber
inherited from NSObject
–bridged–
NSDate
NSData
Data structure in swift
这个重要
class/struct/enum : 3 fundamental building blocks of data structure
Similariries:
1. declaration
2. oproperty and functions
3. initializer
Differences:
1. Inheritance
2. Introspection and casting
3. value type vs. Reference type
Value VS. References
Value
copied && immutable && mutating
Reference
heap && reference counted
Choose
Methods
override &&final(can be class)
types and instances can have methods/properties: (like class and instance methods)
static func abc(d: Double) -> Double
class func abc(d: Double) -> Double
Parameters Names * internal: local var used in method * external: caller used to call method
func foo (external internal :Int)
{
let local = internal
}
fun bar ()
{
let result = foo(external :123)
}
But we seldomly write like above.
-
_ : means not use an external name, default
func foo ( internal :Int)// can be omitted
{
let local = internal
}
fun bar ()
{
let result = foo(123)
} -
#
: force the 1st parameter’s external name to be the internal onefunc foo (#internal :Int)
{
let local = internal
}
fun bar ()
{
let result = foo(internal :123)
} -
not 1st parameter: external is the internal by default; but can be changed
//1
func foo (first :Int, second: Doube)
{
let local = internal
}
fun bar ()
{
let result = foo(123, second: 5.5)
}
//2
func foo (first :Int, external2nd second: Doube)
{
let local = internal
}
fun bar ()
{
let result = foo(123, external2nd: 5.5)
}
//3 also can be ommited, not recommended to to do
func foo (first :Int, _ second: Doube)
{
let local = internal
}
fun bar ()
{
let result = foo(123, 5.5)
}
使用默认的即可,其他的需要知道这种语法,遇到的时候能认识就行
Property
observer
类似KVO
willset didset
var someProperty: Int = 42 {
willSet { newValue}
didSet { oldValue}
}
override var inheriteProperty
{
willSet { newValue }
didSet { oldValue}
}
Lazy Initialization
like dynamic, when in use to access, the var is initialized
lazy var brain = ...
can’t use let
together
may have dependence problems (dependency conundrums)
Initialization
- when is init method needed: usually not
free
init : base class have defaults/struct automaticalled created- what can to do in init: set any property’s value even those have defaults; constant value;self.init();super.init()
- what required in init: all properties must have values; convenience and designated(not convience); desigated can only call a init of its superclass; must init own property before call super.init; must call super.init before assign the inherited value; convenience can only call a designated init in its own and may call desiganated init directly; must call init before set property;
all in all: init is requred before any setting
Inhering init
Required init
Failable init
init?()
{
// may return nil
}
Createing Objects
Any Object
Special type: a protocol
var viewController : AnyObject
var items :[AnyObject]
like id
, Not common use in swift and convert it before using
Casting: as/ as ?
//1
if let viewController = someController as? CalViewController {}// as? returns an optional
//2
if someController is CalViewController {...}
Functions
some Array Methods
+= [T]
first -> T
last -> T
append
slice
insert
remove Index/Range
sort
filter
map
reduce
String
String.Index
is NOT Int
//1
var s = "hello"
let index = advance(s.startIndex, 2)// a String.Index to 3rd glyph, "l"
s.splice("abc",index)// s= "heabcllo"
//2
let num = "56.25"
if let decimalRange = num.rangeOfString(".")
{
let wholeNumberPart = num[num.startIndex..<decimalRange.startindex]
}
Other methods
Notice : NO toDouble
Type Conversion
//1
let d: Double = 37.5
let f: Float = 37.5
let x = Int(d)
let cd = Double(x)
//2: notice here
let a = Array("abc")// a = ["a","b","c"]
let s = String("a","b","c"]) //s = "abc"
Comments