【问题标题】:How does one access the _properties Dictionary object of a Swift 1.2 ACAccount object?如何访问 Swift 1.2 ACAccount 对象的 _properties Dictionary 对象?
【发布时间】:2015-06-17 19:52:51
【问题描述】:

在 XCode 中添加断点向我展示了 Swift 1.2 ACAccount 对象有一个 _properties 对象,该对象又包含 @user_id: 0123456789 的键/值对.我需要访问这个数值。我已阅读文档、Google 搜索、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 的结果。不知道有没有用。

【问题讨论】:

  • valueForKey( "_properties" ) 怎么样?
  • @DarkDust 谢谢,你提醒我错过了一些细节。

标签: swift acaccount


【解决方案1】:

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

    }

【讨论】:

  • 有趣的@airspeed-velocity,这似乎有效。我认为传道书将需要在 21 世纪更新,以使其也阅读“……弱打字有时,强打字有时。”
猜你喜欢
  • 1970-01-01
  • 2013-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-23
  • 2018-09-19
相关资源
最近更新 更多