2016-08-16 43 views
0

我有一個文件格式(.rtf),它包含看起來像數據:轉換.rtf文件到的NSDictionary的NSArray或

[ 
{ 
    "Symbol": "PIH", 
    "Name": "1347 Property Insurance Holdings, Inc.", 
    "LastSale": "6.8299", 
    "MarketCap": 41717882.9375, 
    "ADR TSO": "n/a", 
    "IPOyear": "2014", 
    "Sector": "Finance", 
    "Industry": "Property-Casualty Insurers", 
    "Summary Quote": "http://www.nasdaq.com/symbol/pih", 
    "FIELD10": "" 
}, 
{ 
    "Symbol": "FLWS", 
    "Name": "1-800 FLOWERS.COM, Inc.", 
    "LastSale": "9.27", 
    "MarketCap": 606557031.66, 
    "ADR TSO": "n/a", 
    "IPOyear": "1999", 
    "Sector": "Consumer Services", 
    "Industry": "Other Specialty Stores", 
    "Summary Quote": "http://www.nasdaq.com/symbol/flws", 
    "FIELD10": "" 
}, 
{ 
    "Symbol": "FCCY", 
    "Name": "1st Constitution Bancorp (NJ)", 
    "LastSale": "12.99", 
    "MarketCap": 103169203.98, 
    "ADR TSO": "n/a", 
    "IPOyear": "n/a", 
    "Sector": "Finance", 
    "Industry": "Savings Institutions", 
    "Summary Quote": "http://www.nasdaq.com/symbol/fccy", 
    "FIELD10": "" 
} 
] 

現在,我想這個數據轉換成的東西(的NSDictionary,NSArray中,等),我可以用它來輕鬆訪問數據。任何幫助,將不勝感激。

+0

歡迎堆棧溢出:-) 請看[問] 以及如何創建一個[MCVE。這將有助於獲得有用的答案。 – JimHawkins

回答

0
guard let path = NSBundle.mainBundle().pathForResource("fileName", ofType: "txt") else { 
      return nil 
     } 
     do { 
      let content = try String(contentsOfFile:path, encoding: NSUTF8StringEncoding) 
      return content 
     } catch _ as NSError { 
      return nil 
     } 
  1. 保存.rtf文件轉換成位置(比如桌面)。
  2. 從該位置讀取文件
+0

感謝您的答覆,但它並沒有達到我所需要的程度。相反,我將.rtf文件轉換爲.txt文件(TextEdit使其非常容易),並從此處簡單地序列化它。現在一切正常! –