Today I upgraded Xcode 6 to beta 5 (from beta 1) and as you can imagine I found my previously perfectly working Swift app full of errors of all kind (well, a lot changed from beta 1). Of all errors, there is one I just can't figure out how to fix. It's related to swift closures, in particular the enumerationBlock argument of the .enumerateGroupsWithTypes method. Here is the code:
assetLib.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupSavedPhotos), usingBlock: {
(group: ALAssetsGroup?, stop: CMutablePointer<ObjCBool>) in
...
}, failureBlock: {
(error: NSError!) in
...
})
This did work perfectly in Swift (Xcode 6 beta 1). But now, I get 2 errors:
" 'UnsafeMutablePointer' is not a subtype of 'error type' "
" Use of undeclared type 'CMutablePointer' "
It was clear that CMutablePointer did not exist anymore, so I tried to modify the stop argument like:
..., stop: UnsafeMutablePointer<ObjCBool> ...
After this change, the second error obviously disappeared, but the first transformed in:
" Could not find an overload for 'init' that accepts the supplied arguments "
I even tried to change the UnsafeMutablePointer to a UnsafePointer, as suggested from this post.
EDIT:
Here is the full code of the enumerateGroupsWithTypes method:
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, 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)
})
stop: CMutablePointer<ObjCBool>withstop: UnsafeMutablePointer<ObjCBool>, your code compiles in my project. Is there more code that might cause the problem?NSLog("Error!", nil)is wrong and should beNSLog("Error!").