如何在 swift 中将数组传递给 NSNotification
how to pass array to NSNotification in swift
我有以下代码打印更新图像
的 url
override func viewWillAppear(animated: Bool) {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "assetChange:", name: ALAssetsLibraryChangedNotification, object: nil) }
func assetChange(notification: NSNotification){
if var info:NSDictionary = notification.userInfo { var url:NSSet = info.objectForKey(ALAssetLibraryUpdatedAssetsKey) as NSSet
var aurl:NSURL = url.anyObject() as NSURL
println(aurl)
}
}
这段代码工作正常,但它只会打印第一个修改过的图像url,但我想要所有修改过的图像列表url(修改过的图像数组)请帮助我
您在使用 url.anyObject()
时从集合中选择了一个对象。相反,您需要从集合中获取所有对象,然后遍历数组。以下代码应该对您有所帮助:
func assetChange(notification: NSNotification) {
if var info:NSDictionary = notification.userInfo { var url:NSSet = info.objectForKey(ALAssetLibraryUpdatedAssetsKey) as NSSet
var urls: [NSURL] = url.allObjects as [NSURL]
for singleUrl in urls {
println(singleUrl)
}
}
}
我有以下代码打印更新图像
的 urloverride func viewWillAppear(animated: Bool) {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "assetChange:", name: ALAssetsLibraryChangedNotification, object: nil) }
func assetChange(notification: NSNotification){
if var info:NSDictionary = notification.userInfo { var url:NSSet = info.objectForKey(ALAssetLibraryUpdatedAssetsKey) as NSSet
var aurl:NSURL = url.anyObject() as NSURL
println(aurl)
}
}
这段代码工作正常,但它只会打印第一个修改过的图像url,但我想要所有修改过的图像列表url(修改过的图像数组)请帮助我
您在使用 url.anyObject()
时从集合中选择了一个对象。相反,您需要从集合中获取所有对象,然后遍历数组。以下代码应该对您有所帮助:
func assetChange(notification: NSNotification) {
if var info:NSDictionary = notification.userInfo { var url:NSSet = info.objectForKey(ALAssetLibraryUpdatedAssetsKey) as NSSet
var urls: [NSURL] = url.allObjects as [NSURL]
for singleUrl in urls {
println(singleUrl)
}
}
}