【问题标题】:ABMultiValue address book multi-value properties compile error in SwiftABMultiValue地址簿多值属性在Swift中编译错误
【发布时间】:2015-03-23 20:07:29
【问题描述】:

代码在 Objective-C 中工作,试图转换为 Swift。我在第 10 行提供了有效的 Objective-C 代码和给出编译错误的 Swift 代码(for subValue in aPropertyValue ... 快速枚举器。Swift 错误“类型‘ABMultiValue’不符合协议‘SequenceType’”。

一些ABPerson 属性可能包含多个值(例如多个电话号码)并且是ABMultiValue 类的对象(尽管Xcode 调试器将它们报告为ABMultiValueCoreDataWrapper 类)。附加的 Objective-C 代码循环遍历多个值。我试图在 Swift 中做同样的事情,但无法克服编译错误。两者都是 Xcode 命令行项目。

Objective-C 代码:

#import <Foundation/Foundation.h>
#import <AddressBook/AddressBook.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        ABAddressBook *addressBook = [ABAddressBook sharedAddressBook];
        NSArray *people = [addressBook people];
        for (ABPerson *person in people) {
            for (NSString *aProperty in [ABPerson properties]) {
                id aPropertyValue = [person valueForProperty: aProperty];
                if ([aPropertyValue isKindOfClass: [ABMultiValue class]]) {
                    for (id subValue in aPropertyValue) {
                        NSLog(@"%@",subValue);
                    }
                }
            }
        }
    }
    return 0;
}

快速代码:

import Foundation
import AddressBook
let addressBook = ABAddressBook.sharedAddressBook()
let people = addressBook.people()
for person in people {
    for property in ABPerson.properties() {
        if let aPropertyValue : AnyObject = person.valueForProperty(property as NSString) {
            if aPropertyValue is ABMultiValue {
//                for subValue in aPropertyValue {
                for subValue in aPropertyValue as ABMultiValue {
                    println("\(subValue)") 
                }
            }
        }
    }
}

【问题讨论】:

    标签: macos swift abaddressbook


    【解决方案1】:

    你显然可以使用索引:

    let addressBook = ABAddressBook.sharedAddressBook()
    let people = addressBook.people()
    for person in people {
        for property in ABPerson.properties() {
            if let multiValue = person.valueForProperty(property as NSString) as? ABMultiValue {
                for i in 0 ..< multiValue.count() {
                    println(multiValue.valueAtIndex(i))
                }
            }
        }
    }
    

    或者,按照this answer 中的建议,您可以定义符合SequenceType 的扩展:

    extension ABMultiValue: SequenceType {
        public func generate() -> NSFastGenerator {
            return NSFastGenerator(self)
        }
    }
    

    那么你可以:

    let addressBook = ABAddressBook.sharedAddressBook()
    let people = addressBook.people()
    for person in people {
        for property in ABPerson.properties() {
            if let multiValue = person.valueForProperty(property as NSString) as? ABMultiValue {
                for identifier in multiValue {
                    let value: AnyObject = multiValue.valueForIdentifier(identifier as String)
                    println("\(identifier) : \(value)")
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 2020-12-18
      • 1970-01-01
      • 2018-01-18
      相关资源
      最近更新 更多