objective c - Assigning NSNumber to NSString -
nsstring *string = [nsnumber numberwithint:2]; int var = [string intvalue];
var 2. so, why? understand, it's smth dynamic typing, what's under hood?
there no casting going on in code example: [string intvalue]
regular objective c method provided nsnumber
retrieve value stored inside integer. fact assigned object nsstring
pointer not change anything: object inside remains nsnumber
. not crash because objective c dynamically typed language. lets assign objects of wrong types, if try calling methods of nsstring
on string
object, going crash:
nsstring *string = [nsnumber numberwithint:2]; int var = [string intvalue]; // ok bool istwo = [string isequaltostring:@"2"]; // crash
Comments
Post a Comment