-
Notifications
You must be signed in to change notification settings - Fork 0
斯坦福iOS8学习笔记
@(objc)[读书笔记, iOS]
[toc]
#斯坦福iOS8学习笔记
##第3节 Applying MVC ######1.解决swift中出现的不能重复函数名称的问题,同一函数名称,但参数数量不同
func methodOne() {...}
@nonobjc
func methodOne() {...}
######struct和class的两大区别
name | struct | class |
---|---|---|
继承性 | 不可 | 可以 |
传递性质 | 值 | 引用 |
struct 通常仅用于基础类型数据。 传递参数的前面隐含let。
##第4节 More Swift and Foundation Frameworks #####1.Optional、enum An Optional is just an enum
enum Optional<T>{
case None
case some(T)
}
let x: String? = nil
let x = Optional<String>.None
let x: String? = "hello"
let x = Optional<String>.Some("hello")
var y = x!
switch x{
case Some(let value):y = value
case None //raise an exception
}
#####2.Array
var a = Array<String>()
var a = [String]()
let animals = ["A","B","C"]
var animals = ["A","B","C"]
animals.append("D")
animals[0]
for animal in animals{
}
#####2.Dictionary
var d = Dictionary<String,Int>()
var d = [String:Int]()
d = ["A":1,"B":2 ]
for(key,value) in d{
}
#####3.Range
struct Range<T>{
var startIndex:T
var endIndex:T
}
//a String subrange is not Rang<Int>
let array = ["a","b","c","d"]
let subArray1 = array[2...3]//["c","d"]
let subArray2 = array[2..<3]//["c"]
for i in [23...204]
#####4.NSObject base class for all Objc class. Some advanced features will require you to subclass from NSObject(and it don't hit to do so) #####5.NSNumber
let n = NSNumber(35.5)
let intVersion = n.intValue()
#####6.NSDate
NSCalendar
,NSDateFormatter
,NSDateComponents
#####7.NSData
bag of bits
#####8.classes, Structures and Enumerations
similarities
//declaration syntax
class A{
}
struct B{
}
enum Op{
}
//functions and properties
func doit(argument:argument:Type)->returnValue{
}
var storeProperty = <initial value>(not enum)
var computedProperty:Type{
get{}
set{}
}
//initializer
init(argument1:Type,argument2:Type){
}
enum本身不能存储数据,可以将值存储在枚举的关联信息中,枚举是可以有计算型属性
differences
name | class | struct | enum |
---|---|---|---|
inheritance | √ | × | × |
introspection and casting | √ | × | × |
passing value | reference type | value type | value type |
value type copied when passsed as an argument to a function copied when assigned a different var you must note that any func that can mutable a struct/enum with the keyword mutating
reference type
stored in the heap and reference counted(automatically) constant pointer to a class (let) still can mutable by calling method and changing properties when pass as an argument ,does't make a copy(just passing a pointer to same instance)
class 常用 struct 适合基础数据类型
#####9.method precede your func and var with the keyword override A method can be marked final which will prevent subclasses from being able to override Classed also can be marked final
//parameters names
func foo(external internal:Int){
let local = internal
}
func bar(){
let restart = foo(external:123)
}
func foo(_ internal:Int){
let local = internal
}
func bar(){
let restart = foo(123)
}
//force the first parameter's external name to be the internal name with #
func foo(#internal:Int){
let local = internal
}
#####10.properties property observers you can observe changes to any property with willSet and didSet one very common thing to do in an observer in a Controller is to update the user-interface
var someStoreProperty: Int = 42 {
willSet{ newValue /*is the new value*/}
didSet{ oldValue /*is the old value*/}
}
override var inheritedProperty {
willSet{ newValue /*is the new value*/}
didSet{ oldValue /*is the old value*/}
}
lazy initialization only var a lazy property does not get initialized until someone access it you can allocated an object , excute closure , or a method if you want 可以用来处理错综复杂的初始化依赖
lazy var a = A()//nice if A used a lots of resources
lazy var someProperty:Type = {
return value
}()
lazy var yProperty = self.initializedMyProperty()
#####11.init if a struct has no initializers,it will get a default one with all properties as arguments
struct MyStruct{
var x: Int = 42
var y: String = "moltuae"
init(x:Int, y:String)
}
what can we do inside an init?
you can set any property's value , even those with default values
contant properties (i.e. properties declared with let) can be set
you can all other init methods in your own class using self.init(<args>)
or super.init(<args>)
what are you required to do inside init by the time any init is done, all properties must have values. There are two types of inits in a class , convenience and designated(i.e. not convenience) a designated init must (and can only) call a designated init that is in its immediate superclass. You have to initialize all of your own properties before you call your superclass's initializer. You must call a superclass's init before you assign a value to an inherited property.
A convenience init must (and can only)call a designated init in its own class. A convenience init may call a designated init indirectly ( through another convenience init) A convenience init must call a designated init before it can set any property values.
The calling of other inits must be complete before you can access properties or invoke methods.
other functions
let count = countElements(aCollection)
let sub = dropFirst(aCollection)
let sub = dropLast(aCollection)
let first = first(aCollection)
let last = last(aCollection)
let prefix = prefix(aCollection,X:Int)
let suffix = suffix(aCollection,X:Int)
let reversed:Array = reverse(aCollection)
let backwardsString = String(reverse(s))