1

在 XCode 中添加断点向我展示了 Swift 1.2 ACAccount 对象有一个_properties对象,该对象又包含@user_id: 0123456789的键/值对。我需要访问这个数值。我已经阅读了文档、谷歌搜索、SO'ed 并盲目地尝试了以下所有方法,但均无济于事:

let accountStore = ACAccountStore()
let accountType = accountStore.accountTypeWithAccountTypeIdentifier( ACAccountTypeIdentifierTwitter )
let allACAccounts = accountStore.accountsWithAccountType( accountType )
let someACAccount = allACAccounts[ 0 ]

someACAccount.valueForKey( "properties" )
someACAccount.mutableArrayValueForKey( "properties" )
someACAccount.valueForKeyPath( "properties" )
someACAccount.valueForUndefinedKey("properties")
someACAccount.valueForKey( "user_id" )
someACAccount.mutableArrayValueForKey( "user_id" )
someACAccount.valueForKeyPath( "user_id" )
someACAccount.valueForUndefinedKey("user_id")
someACAccount.properties
someACAccount._properties
someACAccount.user_id

someACAccount.valueForKey( "properties" ) - 拼写为 "properties" 和 "_properties" - 产生 Builtin.RawPointer 的结果。我不知道这是否有用。

4

1 回答 1

3

采用更强类型的方法可能更容易,以下对我来说适用于一个新的 iOS 项目:

    let store = ACAccountStore()
    let type = store.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierTwitter)

    store.requestAccessToAccountsWithType(type, options: nil) { success, error in

        if success, let accounts = store.accountsWithAccountType(type),
            account = accounts.first as? ACAccount
        {
            println(account.username)

            if let properties = account.valueForKey("properties") as? [String:String],
                   user_id = properties["user_id"] {
                    println(user_id)
            }
        }

    }
于 2015-04-12T13:01:31.007 回答