我在 Swift 中使用 hpple,并希望使用相同的语法。
Objective-C 完美运行:
myElement.title = [[element firstChild] content];
Swift 无法编译:
myelement.title = element.firstchild.content
我收到错误 TFHppleElement!没有名为“content”的成员
另一方面,以下工作正常,表明特别是 myElement 和 hpple 似乎设置正确:
myElement.url = element.objectForKey("href") // Works fine
有什么想法吗?
最佳答案
firstchild
可能会返回一个可选的,因为可能没有第一个 child 。您需要打开可选的包装才能使用它。我建议:
if let content = element.firstchild?.content {
myelement.title = content
}
如果 myelement.title
也是可选的,你可以这样做:
myelement.title = element.firstchild?.content
关于ios - 在 Swift 中使用 hpple,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31762606/