ios xcode疑難排解學習筆記(1)

1. try-catch

do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}

ps: 哇靠! swift 的try catch 跟java 不一樣,為啥不能統一。
swift中用do代替try, 然後try又改放在要抓的程式前.
很煩啊,然後會自動產生一個變數error....
基本上這種會自動產生的變數很危險,但swift就是這樣搞...你沒辦法.
java不會發生這種沒宣告就能使用的變數,
他是try- catch (exception e)
這裏e就是一個變數,他不會自動產生一個你沒宣告的變數.
這樣比較安全.

至於:context.save()
這是什麼意思????
core data 的function.
需要產生core data model  xxxx.xcdatamodelId檔案

關於core data可以到 此網站看詳細:

...
關於core data 又一堆需要了解. add/read/write/delete...


2. var TableData:Array< String > = Array < String >()
上面這行是什麼意思?
java與swift不同的地方是,一個是 宣告變數的前後位置不同.
這行的意思是 宣告了一個array,他的型態是字串陣列,名字是TableData.
有點會與java搞混而看不懂.
尤其變數第一個字是小寫比較容易識別,但這裡用大寫T。


3. optionals variable: 
struct UserProfile {
let name: String
let lastName: String
let cellNumber: String
let email: String?
}
swift特別的地方是有optionals variable..
老實說我覺得這特別不好用,
很容易搞混.

他與沒有?的一般變數差在哪?
實在搞不懂...
An optional in Swift is a type that can hold either a value or no value.
就是説一個optionals variable可以是有值或是沒有值...
靠!這到底是什麼意思?
變數本來就是有值跟沒有值兩種,還有第三種???
被這變數搞混了。

來看一般變數在swift:


Only optionals can be nil

If you try to set a non-optional variable to nil, you'll get an error.
var red: String = "Red"
red = nil // error: nil cannot be assigned to type 'String'
如果一個變數,他不是optionals variable, 那他不可以被設做nil.
會出錯.
大概就是這個差別.
你可以試試,在xcode試試,他會出現這樣的錯誤, 連compile都不會過。

留言

這個網誌中的熱門文章

最爛的銀行服務-玉山銀行

Mark App Design Apps - Terms and Privacy Policy (服務條款,隱私權政策)

SMR疊瓦式hdd致命缺陷被解決????!!!

ios app 上架時app icon要注意事項

更改google drive預設存放目錄位置Change Google Drive Default Folder Location in Windows

舊有app在Android 12 閃退問題& app Splash screens

google play 正式發布前測試報告...非常好用.

app bundle and bundletool. 關於aab安裝問題

關於google play console app應用程式簽署

Google Play badge徽章產生器