iOS 入门常见问题
1. Device os version lower than deployment target
我把手机连接电脑后,准备跑项目,但是Xcode提醒Device os version lower than deployment target
原因是项目要求最低版本高于手机版本,单击项目修改Deployment Target即可
APIhttps://developer.apple.com/library/ios/navigation/
学习视频http://open.163.com/special/opencourse/ios8.html
/Users/maizhikun/Learning/iOS/workspace/Calculatoer/Calculatoer/CalculatorBrain.swift:12:22: ‘Printable’ has been renamed to ‘CustomStringConvertible’
enum Op: Printable改成
enum Op: CustomStringConvertible
书籍
http://www.zhihu.com/question/24004342
学习博客
白胡子教程笔记
第四课Swift和Foundation框架
数组
var a = Arry<String>() var a = [String]() var animals = ["Giraffe","Cow"] animals.append("Dog"); let animal = animals[1] for animals in animals { println("\(animal)") }
Dictonary
var pac10teamRankings = Dictionary<String, Int>() var pac10teamRankings = [String:Int]() pac10teamRankings = ["Stanford":1,"Cal":10] let ranking = pac10teamRankings["Ohin State"] //return nil for (key , value ) in pack10teamRankings { println("\(key) = \(value)") }
Rabge
struct Range<T>{ var startIndex :T var endIndex: T }
An Array’s range would be a Range
Warning: A String subrange is not Range
(it is Range<String.Index> … we’ll talk later!) There is special syntax for specifying a Range: either …(ubclusive) or ..<(open-ended)
let array =["a","b","c","d"] let subArray1 = array[2...3] //return ["c","d"] let subArray2 = array[2..<3] // return ["c"] for i in [27...104]{ }
其他数据类型
NSObject NSNumber NSDate NSData
数据类型
三者不同点://声明 class CalculatorBrain { } struct Vertex {} enum Op {} //函数 func doit(argument: Type) -> ReturnValue{} var storedProperty =<initial value>(not enum) var computerProperty: Type{ get {} set {} } //构造函数 init(argument1: Type, argument2: Typ2, ...){}
- Inheritance(class only)
- Introspection and castring(class only)
- Value type(struct,enum) vs. Referenct type(class)
Value vs. Referenct
Value:
You must note any func that can mutate a struct/enum with the keyword
mutaint
Referenct :
Methods
重写父类方法/属性时,要使用
override
final
标记不能被重写的方法和属性var d: Double =... if d.isSignMinus { d =Double.abs(d) } static func abs(d: Double) -> Double{} //Parameter Names 1. func foo(external internal: Int){ let local = internal } func bar() { let result = foo(external: 123) } 2.我部想拥有一个外部名 func foo(_ internal: Int){ let local = internal } func bar(){ let result = foo(123) } 3. 必须要外部名 func foo(#internal: Int){ let local = internal } func bar(){ let result = foo(internal: 123) } 4. func foo(first: Int, second: Double){ let local =internal } func bar(){ let result = foo(123, second 5.5) } 5. fimc fpp(first: Int, externalSecond second: Double){ let local =internal //?这是不是错了? } func bar(){ let result = foo(123, externalSecond: 5.5) } 6. func foo(first: Int, _ second: Double){ let local = internal } func bar(){ let result =foo(123,5.5) }
Properties
var someStoredProperty: Int =42{ willset { newValue is the new value } didSet {oldValue is the old value } } var inheritedProperty { willSet { newValue is the new value} didSet { oldValue is the old value} }
Lazy Initialization (惰性实例化)
只有var 可以用lazy ,let不可以
lazy var brain = Calculator Brain() lazy var someProperty: Type = { return <the constructed value > } lazy var myProperty =self.initializeMyProperty()
Initialization
lazy 或者其他很多方法可以避免initiative
两种构造方法selfinit(<args>) super.init(<args>)
1. Convenience Init 1. 只能调用本类的designated initializer 2. 只能通过本类的designated intializer才能访问其他的值 2. Designated Initalizers(default) 1. 之前不能有Convenience Init 2. 只能调用其父类的Designated initializer 3. 调用父类的Init前,先初始化所有的property 4. 必须先让父类初始化property才能给子类调用
子类没写designated init则继承父类所有的designated init和Converienct init,否则不继承父类的所有designated init
required init 子类必须实现这个方法
init?(arg1: Type1, ...){} let image = UIImage(named: "foo") if let image = UIImage(named: "foo"){ successfully }else { faled } let commaSeparatedArrayElements: String = ",".join(myArray)
AnyObject
主要用来兼用现有的Object-C和iOS代码
var destinationViewController: AnyObject var toolbarItems: [AndObject] func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject) func appConstraints(constraints: [AnyObject]) func appendDigit(sender: AnyObject) class func buttonWithType(buttonType: UIButtonType) -> AnyObject let calcVC = destinationViewController as CalculatorViewController if let calcVC = destinationViewController as? CalculatorViewController {...} --- var toolbarItems: [AnyObject] for item in toolbarItems { if let toolbarItem = item as? UIBarButtonItem{ } } for toolbarItem in toolbarItems as [UIBarButtonItem] { //不能使用as? 因为for toolbarItem in nil是无意义的 } --- let button: AnyObject =UIButton.buttonWithType(UIButtonType.System) let tutle = (button as UIButton).currentTitle
11 Function
//Some Array<T> Methods
+=[T] //not +=T
var a = [a,b,c]
append(T)
insert(T, atIndex : Int)
splice(Array<T>,atIndex: Int) a.splice([d,e], atIndex:1),a = [a,d,e,b,c]
removeAtIndex(Int)
removeRange(Range) a.removeRange(0..<2) a = [c]
replaceRange(Range, [T]) //a.replaceRange(0...1 with: [x,y,z]),a= [x,y,z,b]
sort(isOrderedBefore : (T,T)) -> Bool) //e.g a.sort($$0 <$1)
filter(includeElement: (T) -> Bool) -> [T]
let stringified: [String] = [1,2,3].map{ "\($0)"}
let sum: Int = [1,2,3].reduce(0) {$0 + $1}
//String
var s = "hello"
let index = advance(s.startIndex,2) //return l
s.splice("abc",index) // return heabcllo
let startIndex =advance(s.startIndex,1)
let endIndex = advance(s.startIndex,6)
let substring = s[index..<endIndex] // return eabcl
//String.Index
let num = "56.25"
if let decimalRange = num.rangeOfString("."){
let wholeNumberPart = num[num.startIndex..<decimalRange.startIndex]
}
s.removeRange([s.startIndex..<decimalRange.startIndex])
//Other String Methods
description ->String //Printable
endIndex -> String.Index
hasPrefix(String)) -> Bool
hasSuffix(String) -> Bool
toInt -> Int? //not toDouble
capitalizedString -> String
lowercaseString -> String
uppercaseString -> String
join(Array) -> String // ",".join(]"1","2",3""]) = "1,2,3"
componentsSeparatedByString(Strign ) -> [String] // "1,2,3".csbs(",") = ["1","2","3"]
//Type Conversion
let d: Double =37.5
let f: Falot = 37.5
let x = Int(d)
let xd = Double(x)
let cgf =CgFloat(d)
let a = Array("abc") // a = ["a","b","c"]
let s = String(["a","b","c"]) // return s = "abc"
let s = String(52)
let s= "\(37.5)"
//Assertions
assert(() -> Bpp; ,"message")
e.g: assert(validation() !=nil,"the validation function returned nil")
//Other Functions
let count = countElements(aCollection) //how many elements in the collection
let sub = dropFirst(aSliceable) //drops the first thing in the sliceable
let sub dropLast(aSliceable) // drops the last thing in the sliceable
let first = first(aCollection)
let last = last(aCollection)
let prefix = prefix(aSliceable, X:Int)
let suffix = suffix(aSliceable, X: Int)
let reversed: Array = reverse(aCollection)
let backwardsSTRING = String(reverse(s))