Search code examples
iosswifthpple

Using hpple in Swift


I am using hpple in Swift and would like to use the same syntax.

Objective-C works perfectly:

myElement.title = [[element firstChild] content];

Swift does not compile:

myelement.title = element.firstchild.content

I receive the error TFHppleElement! does not have a member named 'content'

On the other hand, the following works fine which shows that specifcally myElement and hpple seem to be set up correctly:

myElement.url = element.objectForKey("href")  // Works fine

Any ideas?


Solution

  • firstchild is likely returning an optional since there may be no first child. You'll need to unwrap that optional to use it. I suggest:

    if let content = element.firstchild?.content {
        myelement.title = content
    }
    

    If myelement.title is also an optional, you could do:

    myelement.title = element.firstchild?.content