Skip to main content
 首页 » 编程设计

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

2024年01月01日56arxive

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

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 的结果。不知道有没有用。

请您参考如下方法:

采用更强类型的方法可能更容易,以下方法适用于我的新 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) 
            } 
        } 
 
    }