xcode - How to initialize a ALAssetsGroupType constant in Swift? -


i'm trying initialize alassetsgrouptype constant in swift (xcode 6.4.):

let grouptypes: alassetsgrouptype = alassetsgrouptype(alassetsgroupall) 

but doesn't compile 32bit devices(ex, iphone 5) , error: enter image description here

there's better way, direct approach use constructor int32 create signed int32 uint32:

let grouptypes: alassetsgrouptype = alassetsgrouptype(int32(bitpattern: alassetsgroupall)) 

explanation

if option-click on alassetsgrouptype see typealias int:

typealias alassetsgrouptype = int 

but, if click on assetslibrary next declared in see in header file typedef nsuinteger:

alassetslibrary.h

typedef nsuinteger alassetsgrouptype; 

so, what's going on here? why doesn't swift treat nsuinteger uint? swift typed language, means can't assign int uint without conversion. keep our lives simpler , remove many of conversions, swift engineers decided treat nsuinteger int saves lot of hassle in cases.

the next piece of mystery definition of alassetsgroupall:

enum {     alassetsgrouplibrary        = (1 << 0),         // library group includes assets.     alassetsgroupalbum          = (1 << 1),         // albums synced itunes or created on device.     alassetsgroupevent          = (1 << 2),         // events synced itunes.     alassetsgroupfaces          = (1 << 3),         // faces albums synced itunes.     alassetsgroupsavedphotos    = (1 << 4),         // saved photos album. #if __iphone_5_0 <= __iphone_os_version_max_allowed     alassetsgroupphotostream    = (1 << 5),         // photostream album. #endif     alassetsgroupall            = 0xffffffff,       // same oring available group types, }; 

note comment next alassetsgroupall says "the same oring available group types". well, 0x3f have sufficed, presumably author decided set all of bits future proof in case other options added in future.

the problem while 0xffffffff fits in nsuinteger, doesn't fit int32, overflow warning on 32-bit systems. solution provided above converts uint32 0xffffffff int32 same bitpattern. gets converted alassetsgrouptype int, on 32-bit system int bits set (which representation of -1). on 64-bit system, int32 value of -1 gets sign extended -1 in 64-bit sets 64 bits of value.

another way solve define own allgroups:

let allgroups = -1  // bits set let grouptypes: alassetsgrouptype = allgroups 

note, deprecated in ios 9:

typedef nsuinteger alassetsgrouptype ns_deprecated_ios(4_0, 9_0, "use phassetcollectiontype , phassetcollectionsubtype in photos framework instead"); 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -