<WARNING> -[Twitter APIClient] will soon be deprecated
Xcode 7.2 Build version 7C68
Apple Swift version 2.1.1 (swiftlang-700.1.101.15 clang-700.1.81)
Fabric 2.1.2
Twitter 1.14.6
2016-01-11 00:18:41.312 ***[9526:4748161]
-[Twitter APIClient] will soon be deprecated. This method can return unexpected results based on who is logged in or logged out. It is recommended that users use -[TWTRAPIClient initWithUserID:] for more explicit control over which user is making requests.
//before Twitter.sharedInstance().APIClient.sendTwitterRequest(request, completion: completion) //after let client = TWTRAPIClient(userID: userID) client.sendTwitterRequest(request, completion: completion)
ちなみに僕はSession取得用のfuncを作って使ってます。
func getUserSession() -> TWTRAuthSession? { let sessionStore = Twitter.sharedInstance().sessionStore if let session = sessionStore.session() { return session }else{ return nil } } let userSession = self.getUserSession() let client = TWTRAPIClient(userID: userSession!.userID)
Fabric - Swift2 : 認証時のエラーなど
Twitter認証エラーの時にNSErrorで得られるcodeはEnumで以下のように定義されている
/** * Error codes surfaced by the Twitter SDK with the `TWTRLogInErrorDomain` error domain. */ public enum TWTRLogInErrorCode : Int { /** * Unknown error. */ case Unknown /** * User denied login. */ case Denied /** * User canceled login. */ case Canceled /** * No Twitter account found. */ case NoAccounts /** * Reverse auth with linked account failed. */ case ReverseAuthFailed /** * Refreshing session tokens failed. */ case CannotRefreshSession /** * No such session or session is not tracked * in the associated session store. */ case SessionNotFound /** * The login request failed. */ case Failed /** * The system account credentials are no longer valid and the * user will need to update their credentials in the Settings app. */ case SystemAccountCredentialsInvalid }
だから例えば認証のキャンセル時はアラートを出したくない場合は以下のように判定すれば良い
if error!.code != TWTRLogInErrorCode.Canceled.rawValue { ... }
rawValue はSwiftの列挙型のメンバに割り当てられたInt値を返却してくれる
Swift2 : クロージャの省略形について
クロージャの省略形
次のようなクロージャを例に見比べてみましょう。
var myFunc : (i:Int, j:Int) -> Int
1.0. 省略なし
まずは省略しない記述。
myFunc = { (i:Int, j:Int) -> Int in return i + j }
1.1. 型の省略が可能なパターン
型が推論できるため省略できる。
myFunc = { (i, j) in return i + j }
1.2. return文の省略が可能なパターン
処理内容がreturn文の一行のみであるため省略する。
myFunc = { (i, j) in i + j }
引数としてのクロージャ
2.1. クロージャ1個のみを引数とする場合の( )が省略可能
let x:Int = 100, y:Int = 200 let hoge = { (f:((Int, Int) -> Int)) -> Int in return f(x, y) } let ans = hoge {$0 + $1}
2.2. 最後の引数がクロージャの場合は引数の外に出せる
こちらは省略形ではありませんが覚えておいて損はないでしょう。
let calc = { (i:Int, j:Int, f:((Int, Int) -> Int)) -> Int in return f(i, j) } let ans = calc(1, 5) {$0 + $1}