Skip to content

Instantly share code, notes, and snippets.

@dasmer
Created February 10, 2016 03:49
Show Gist options
  • Save dasmer/66a3549d13d41f690649 to your computer and use it in GitHub Desktop.
Save dasmer/66a3549d13d41f690649 to your computer and use it in GitHub Desktop.
Invalid Type Cast Objective C
// Suppose `urls` is an property of a class of type NSArray and it contains NSURLs
NSString *string = self.urls[0];
NSString *firstCharacter = [string subtringToIndex:1];
// This would break at runtime because we casted it to the wrong value type.
// We can get around this by creating NSArrays that are generic over NSURL. That way we would get a warning on the NSString cast.
// I may also mention that NSArrays can store objects of different types unlike Swift where all the object must be of the same type.
// That said, objects that contain multiple types that do not all inherit from the same object or conform to the same protocol usually should never be in the same NSArray
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment