如何快速将数组传递给 NSNotification

Posted

技术标签:

【中文标题】如何快速将数组传递给 NSNotification【英文标题】:how to pass array to NSNotification in swift 【发布时间】:2015-03-16 10:52:22 【问题描述】:

我有以下代码打印更新图像的 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 列表(修改后的图像数组)请帮助我

【问题讨论】:

【参考方案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)
           
    

【讨论】:

以上是关于如何快速将数组传递给 NSNotification的主要内容,如果未能解决你的问题,请参考以下文章