如何在swift中将数组传递给NSNotification

时间:2015-03-16 10:52:23

标签: swift nsnotificationcenter alassetlibrary

我有以下代码打印更新图像的网址

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)

     }

}

这段代码工作正常,但它只打印第一个修改过的图片网址,但是我想要修改后的图片网址列表(修改过的图像数组)请帮帮我

1 个答案:

答案 0 :(得分:1)

使用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)
        }   
    }
}