如何访问Swift 1.2 ACAccount对象的_properties Dictionary对象?

时间:2015-04-12 12:31:10

标签: swift acaccount

在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的结果。我不知道这是否有用。

1 个答案:

答案 0 :(得分: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)
            }
        }

    }