今天我将
Xcode 6升级到beta 5(来自beta 1),你可以想象我发现我之前完美运行的
Swift应用程序充满了各种错误(好吧,从beta 1有很多变化).在所有错误中,有一个我无法弄清楚如何修复.它与swift闭包有关,特别是.enumerateGroupsWithTypes方法的enumerationBlock参数.这是代码:
assetLib.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupSavedPhotos),usingBlock: { (group: ALAssetsGroup?,stop: CMutablePointer<ObjCBool>) in ... },failureBlock: { (error: NSError!) in ... })
这确实在Swift(Xcode 6 beta 1)中完美运行.但现在,我得到2个错误:
>
” ‘UnsafeMutablePointer’ is not a subtype of ‘error type’ “
>
” Use of undeclared type ‘CMutablePointer’ “
很明显,CMutablePointer不再存在,所以我试图修改stop参数,如:
...,stop: UnsafeMutablePointer<ObjCBool> ...
在这个改变之后,第二个错误显然消失了,但第一个转变为:
” Could not find an overload for ‘init’ that accepts the supplied arguments “
我甚至尝试将UnsafeMutablePointer更改为UnsafePointer,如this post所示.
编辑:
以下是enumerateGroupsWithTypes方法的完整代码:
assetLib.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupSavedPhotos),usingBlock: { (group: ALAssetsGroup?,stop: UnsafeMutablePointer<ObjCBool>) in if group != nil { group!.setAssetsFilter(ALAssetsFilter.allPhotos()) group!.enumerateAssetsAtIndexes(NSIndexSet(index: group!.numberOfAssets()-1),options: nil,usingBlock: { (result: ALAsset!,index: Int,stop: UnsafeMutablePointer<ObjCBool>) in if result { var alAssetRapresentation: ALAssetRepresentation = result.defaultRepresentation() url = alAssetRapresentation.url() } }) } else if group == nil { assetLib.assetForURL(url,resultBlock: { (asset: ALAsset!) in if asset != nil { var assetRep: ALAssetRepresentation = asset.defaultRepresentation() var iref = assetRep.fullResolutionImage().takeUnretainedValue() var image = UIImage(CGImage: iref) imageView.image = image self.view.addSubview(imageView) let mask = CAShapeLayer() mask.path = UIBezierPath(ovalInRect: CGRectMake(0,200,200)).CGPath mask.frame = CGPathGetPathBoundingBox(mask.path) mapView.layer.mask = mask self.view.addSubview(mapView) } },failureBlock: { (error: NSError!) in NSLog("Error!",nil) }) } },failureBlock: { (error: NSError!) in NSLog("Error!",nil) })