7

I try to use the enumerateGroupsWithTypes method of the ALAssetsLibrary class but I get an error with the first parameter.

The method's prototype :

func enumerateGroupsWithTypes(types: ALAssetsGroupType,
    usingBlock enumerationBlock: ALAssetsLibraryGroupsEnumerationResultsBlock!,
    failureBlock: ALAssetsLibraryAccessFailureBlock!)

How I call this method :

assetLib.enumerateGroupsWithTypes(ALAssetsGroupAll, usingBlock: success, failureBlock: fail)

but I get a compile error 'CUnsignedInt' is not convertible to 'ALAssetsGroupType'

Other tests :

Based on what I've found on internet and my own tests, I've also tried

Test 1

assetLib.enumerateGroupsWithTypes(ALAssetsGroupAll as ALAssetsGroupType, usingBlock: success, failureBlock: fail)

And the result is a compile error Cannot convert the expression's type 'Void' to type 'ALAssetsGroupType'

Test 2

assetLib.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupAll), usingBlock: success, failureBlock: fail)

And the result is a runtime error EXC_BAD_ACCESS and an XCode crash.

Morniak
  • 958
  • 1
  • 12
  • 37
  • Isn't it an enum? If so, you need to pass in `ALAssetsGroup.All` or just `.All`. – Jack Jul 09 '14 at 14:18
  • 2
    It's not an enum as in Objective-C : `var ALAssetsGroupAll: CUnsignedInt { get }` `typealias ALAssetsGroupType = Int` – Morniak Jul 09 '14 at 14:20
  • 2
    I see...looks like one of those cases where they used `enum` and not `NS_ENUM` and the translator broke...bug report! – Jack Jul 09 '14 at 14:28

2 Answers2

1

Looks like the correct way is to use ALAssetsGroupType's initializer to create a new ALAssetsGroupType. The following should work:

assetLib.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupAll), usingBlock: success, failureBlock: fail)
Mike Akers
  • 12,039
  • 14
  • 58
  • 71
-1

This seems to compile:

Although, it's untested as I don't know exactly what it returns with allZeros.

It may return 0x00000 as opposed to what you need, which is 0xFFFFFF.

Still might be a good reference for people in the future.

let assetLib = ALAssetsLibrary()
assetLib.enumerateGroupsWithTypes(ALAssetsGroupType.allZeros, usingBlock: { (results, stop) in
    println(results)
}, failureBlock: { (fail) in
    println(fail)
})

EDIT:

Well, what happens if you do the following?

assetLib.enumerateGroupsWithTypes(0xFFFFFFFF, usingBlock: ...)

I get the following:

(ALAssetsGroup - Name:Instagram, Type:Album, Assets count:46, )
(ALAssetsGroup - Name:Snapchat, Type:Album, Assets count:0, )
(ALAssetsGroup - Name:Camera Roll, Type:Saved Photos, Assets count:681, )
(nil, )

It's probably a bug like Jack Wu mentioned where they probably used enum and not NS_ENUM and the translator broke.

Doing this might be a workaround for the time being.

Types of Asset

Constants to identify types of asset.

enum {
   ALAssetsGroupLibrary        = (1 << 0),
   ALAssetsGroupAlbum          = (1 << 1),
   ALAssetsGroupEvent          = (1 << 2),
   ALAssetsGroupFaces          = (1 << 3),
   ALAssetsGroupSavedPhotos    = (1 << 4),
   ALAssetsGroupPhotoStream    = (1 << 5),
   ALAssetsGroupAll            = 0xFFFFFFFF,
};
Community
  • 1
  • 1
gotnull
  • 26,454
  • 22
  • 137
  • 203
  • `ALAssetsGroupType.allZeros` return `0x00000` while the value of the option that I want to use is `0xFFFFFF`. So it compile but don't produce the expected result (none of the bit is set so the result will always be an empty array). – Morniak Jul 10 '14 at 08:43
  • @Morniak See my updated answer. Have you tried `0xFFFFFFFF` as the `ALAssetsGroupType`? – gotnull Jul 11 '14 at 08:46
  • Yes,it work and this is what I'm doing but it's not a viable solution. `0xFFFFFFFF` is a magic number and if for any reason apple change the values of this enum my app will be broken. – Morniak Jul 11 '14 at 09:06
  • @Morniak: Yeah, that's correct. That is why I mentioned doing this might be a workaround for the time being. – gotnull Jul 14 '14 at 01:04