###前提・実現したいこと
RSSリーダーアプリを作成しています。
こちらを参考に進めていました。
http://x1.inkenkun.com/archives/909
あらかた、ライブラリ等を入れて、
ビルドしましたが、エラーが出ました。
###発生している問題・エラーメッセージ
if let ogTags = html.nodesMatchingSelector("meta[property=\"og:description\"]") { と、 if let imgTags = html.nodesMatchingSelector("img") { のコードについて、 以下のエラーメッセージ Initializer for conditional binding must have Optional type , not '[HTMLElemet]'
###該当のソースコード
Swift
1 2 3import UIKit 4import Alamofire 5import SwiftyJSON 6import HTMLReader 7 8class parseFeed{ 9 10 /* 11 RSSのJSONをパースする 12 */ 13 func parse (url: String, completion: (([JSON]?, NSError?) -> Void)){ 14 15 var url = NSURL(string: url) 16 17 Alamofire.request(.GET, url!, parameters: nil, encoding: .JSON) 18 19 .responseJSON {(response) in 20 21 let json = JSON(response.data!) 22 let entries = json["responseData"]["feed"]["entries"].array 23 24 completion(entries, response.result.error) 25 26 } 27 28 29 30 31 32 } 33 34 35 /* 36 URL先の文章と画像を取得 37 */ 38 func getContents (url: String, completion: ((AnyObject, NSError?) -> Void)){ 39 40 var url = NSURL(string: url) 41 var ret : Dictionary<String, String!> = [:] 42 43 Alamofire.request(.GET, url!, parameters: nil) 44 45 .responseString { (response) in 46 47 48 let data = JSON(response.data!) 49 50 var content = "" 51 let html = HTMLDocument(string: String(response.data)) 52 53 54 55 if let ogTags = html.nodesMatchingSelector("meta[property=\"og:description\"]") { 56 for tag in ogTags { 57 content = (tag.attributes?["content"] as? String)! 58 } 59 } 60 61Initializer for conditional binding must have Optional type , not '[HTMLElemet]' 62 63 64 65 var image = "" 66 if let imgTags = html.nodesMatchingSelector("img") { 67 for img in imgTags { 68 if(img.attributes?["data-src"] != nil){ 69 image = (img.attributes?["data-src"] as? String)! 70 } 71 } 72 } 73 74 ret = [ "content": content , "image" : image ] 75 completion(ret,response.result.error) 76 } 77 } 78 79} 80 81 82
###試したこと
同様のエラーの検索を行い、
http://hacknote.jp/archives/16247/
以下の方法を見つけたが、
宣言の仕方がわからない。
他、
[Swift][Optional] アンラップしているはずなのに、アンラップされない
http://qiita.com/cotrpepe/items/3958a6863f1e412073e1
または、別の方法を模索しています。
エラーの解消を行うための、修正コードを教えて頂きたいです。
よろしくお願いします。
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/08/15 03:57 編集
2016/08/16 12:09
2016/08/20 15:16