学习 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>
快速执行此操作的正确方法是什么?我使用的每个 for 循环都会返回无下标的错误。
最佳答案
根据您所拥有的信息,您将如何使用 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,请将所有防护更改为 if let 构造,并且防护的 else 部分之后的任何内容都应进入 if let 构造内部。如果您有任何相关问题,请告诉我。 URLs 数组将包含您要查找的字符串形式的 URL。
关于swift - 在 Swift 中使用复杂的 .plist 对 NSDictionary 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31191520/