我无法在Swift 3中枚举NSAttributed文本

时间:2016-10-21 23:02:02

标签: ios swift

我无法在swift 3中正确编译。我遇到了

的问题

enter image description here

此处的实际代码。我不知道为什么要在代码

中要求额外费用
 data2 = items.data(using: .utf8)
            attrString = NSAttributedString(htmlData:data2!,options:[DTDefaultFontSize:13.0,DTDefaultFontFamily:"Verdana",DTDefaultFirstLineHeadIndent:5.0],documentAttributes:nil)
            print(attrString)

            attrString?.enumerateAttribute(NSAttachmentAttributeName , in: NSMakeRange(0, (attrString?.length)!), options: 0, using:^(id value,NSRange range,BOOL *test){
                if(value){
                    print(value)
                }
                })

1 个答案:

答案 0 :(得分:1)

您必须复制Objective C示例,而不将其完全转换为swift。块语法和 if(value)之类的东西都是有效的目标C但不是有效的swift。以下代码在Swift playground中正常运行:

    let attrString = NSAttributedString(string: "test", attributes: [NSForegroundColorAttributeName : UIColor.red, NSUnderlineColorAttributeName : UIColor.green])
    attrString.enumerateAttribute(NSForegroundColorAttributeName , in: NSMakeRange(0, attrString.length), options: [.longestEffectiveRangeNotRequired]) { value, range, isStop in
        if let value = value {
            print(value)
        }
    }