学习Swift并在解析通过作为NSDictionary的plist时遇到一些麻烦。
我正在构建一个测试应用程序,它从〜/ Library / Safari / Bookmarks.plist中提取Safari的阅读列表的URLString。
我可以使用以下内容读取xCode中的数据:
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
let location = "~/Library/Safari/Bookmarks.plist".stringByExpandingTildeInPath
var error: NSError?
var plistArray = NSDictionary(contentsOfFile: location)
println(plistArray)
}
我所拥有的困难是能够通过字典上的循环来协调并从阅读列表中提取我需要的值。以下是plist的格式。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Children</key>
<array>
<dict>
<key>Title</key>
<string>History</string>
<key>WebBookmarkIdentifier</key>
<string>History</string>
<key>WebBookmarkType</key>
<string>WebBookmarkTypeProxy</string>
<key>WebBookmarkUUID</key>
<string>77F746D5-FAE9-453F-BB61-20C6709321C3</string>
</dict>
<dict>
<key>Children</key>
<array>
<dict>
<key>URIDictionary</key>
<dict>
<key>title</key>
<string>Yelp</string>
</dict>
<key>URLString</key>
<string>http://www.yelp.com/</string>
<key>WebBookmarkType</key>
<string>WebBookmarkTypeLeaf</string>
<key>WebBookmarkUUID</key>
<string>5EF3DFF8-909D-400C-9738-78EC9BCDB8FD</string>
</dict>
</array>
<key>Title</key>
<string>BookmarksBar</string>
<key>WebBookmarkType</key>
<string>WebBookmarkTypeList</string>
<key>WebBookmarkUUID</key>
<string>81E022A4-9F93-4D52-8516-29D4833349F2</string>
</dict>
<dict>
<key>Title</key>
<string>BookmarksMenu</string>
<key>WebBookmarkType</key>
<string>WebBookmarkTypeList</string>
<key>WebBookmarkUUID</key>
<string>522EFC56-F2B2-400D-B718-6C9430473FD8</string>
</dict>
<dict>
<key>Children</key>
<array>
<dict>
<key>ReadingList</key>
<dict>
<key>DateAdded</key>
<date>2015-07-02T17:50:27Z</date>
<key>PreviewText</key>
<string>Overview This article explains how to start and stop Tomcat on the JSS host server. There are different ways to start and stop Tomcat depending on the version of the Casper Suite that you are running and the platform on</string>
</dict>
<key>ReadingListNonSync</key>
<dict>
<key>AddedLocally</key>
<true/>
<key>ArchiveOnDisk</key>
<true/>
<key>DateLastFetched</key>
<date>2015-07-02T17:50:30Z</date>
<key>FetchResult</key>
<integer>1</integer>
</dict>
<key>URIDictionary</key>
<dict>
<key>title</key>
<string>Starting and Stopping Tomcat</string>
</dict>
<key>URLString</key>
<string>https://jamfnation.jamfsoftware.com/article.html?id=117</string>
<key>WebBookmarkType</key>
<string>WebBookmarkTypeLeaf</string>
<key>WebBookmarkUUID</key>
<string>0F579C8E-FD60-4AF2-92F3-AC6929465B6F</string>
</dict>
</array>
<key>ShouldOmitFromUI</key>
<true/>
<key>Title</key>
<string>com.apple.ReadingList</string>
<key>WebBookmarkType</key>
<string>WebBookmarkTypeList</string>
<key>WebBookmarkUUID</key>
<string>9406BEF1-FEED-41EE-AF05-AA599B9DD7B1</string>
</dict>
</array>
<key>Title</key>
<string></string>
<key>WebBookmarkFileVersion</key>
<integer>1</integer>
<key>WebBookmarkType</key>
<string>WebBookmarkTypeList</string>
<key>WebBookmarkUUID</key>
<string>A9E09D59-9251-406A-B743-42E5B75FC170</string>
</dict>
</plist>
我正在尝试获取阅读列表部分中链接的URL。具体做法是:
<string>https://jamfnation.jamfsoftware.com/article.html?id=117</string>
在swift中执行此操作的正确方法是什么?我使用的每个for循环都返回无下标的错误。
答案 0 :(得分:1)
根据您的具体内容,您将如何使用Swift 2:
let location = "~/Library/Safari/Bookmarks.plist".stringByExpandingTildeInPath
guard let childrenArray = NSDictionary(contentsOfFile: location)?.objectForKey("Children") as? [[NSObject : AnyObject]]
else
{
// TODO: Handle error file couldn't be read.
return
}
var URLs = [String]()
for child in childrenArray
{
guard let subChildren = child["Children"] as? [[NSObject: AnyObject]]
else { continue }
for subChild in subChildren
{
guard let URL = subChild["URLString"] as? String
else { continue }
URLs.append(URL)
}
}
print(URLs)
// The URLs array will have the link you are looking for.
如果你没有使用swift 2改变所有的守卫,如果让构造和任何来自守卫的else部分后面的东西都应该进入if let构造。如果您有任何相关问题,请与我们联系。 URL数组将包含您要查找的字符串。