ios - swift forcing objective-c int to be assigned as Int32 then crashing -


i have objective c property has been declared

@property int xmbuffersize; 

if sharedexample.xmbuffersize = 1024 works fine when trying set integer value property variable

var getthat:int = dict["buffersize"]!.integervalue sharedexample.xmbuffersize = getthat 

it can't above

cannot assign value of type 'int' value of type 'int32'

if force

sharedexample.xmbuffersize =dict["buffersize"] as! int32 

it crashing error

could not cast value of type '__nscfnumber' 'swift.int32'

edit::::
dict init, there other objects in dict besides integers

var buffersize:int = 1024 var dict = dictionary<string, anyobject>() = ["buffersize":buffersize] 

the value in dict nsnumber, cannot cast or directly converted int32. can first obtain nsnumber , call intvalue on it:

if let buffersize = dict["buffersize"] as? nsnumber {     sharedexample.xmlbuffersize = buffersize.intvalue } 

the if let … as? allows verify value indeed nsnumber, since (as said) there can other types of objects in dict. then-branch execute if dict["buffersize"] exists , nsnumber.

(note: can try integervalue if intvalue gives wrong type, or convert resulting integer – cint(buffersize.integervalue) – needed. swift doesn't implicit conversions between different integer types, need match exactly.)


Comments