rangeOfString method call causing unrecognized selector error - ios

I am accessing the address book contacts, and then running a for loop on all of the contacts. The loop's main purpose is to access the First Name and Phone Number values of the user's address book contacts and place those values into NSString objects.
All of the above works perfectly. My problem is that I need to execute specific code if the phone number data contains this string: _$!<Mobile>!$_
So I setup a rangeOfString method call on the object that contains the phone number data and this is causing problems.
Here is what prints to the console once I added the rangeOfString method call to my code:
2014-01-18 12:10:49.190 PhotoTest1[1244:60b] -[__NSCFType rangeOfString:]: unrecognized selector sent to instance 0x14e9ac90
2014-01-18 12:10:49.193 PhotoTest1[1244:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType rangeOfString:]: unrecognized selector sent to instance 0x14e9ac90'
*** First throw call stack:
(0x2f8f6f4b 0x39d376af 0x2f8fa8e7 0x2f8f91cb 0x2f8fa478 0x55e29 0x3206e37b 0x3206e139 0x32117e27 0x3215449b 0x32152dd3 0x32151e25 0x3232dcb3 0x3209e713 0x3209e6b3 0x3209e691 0x3208a11f 0x3209e107 0x3209ddd9 0x32098e65 0x3206e79d 0x3206cfa3 0x2f8c2183 0x2f8c1653 0x2f8bfe47 0x2f82ac27 0x2f82aa0b 0x34551283 0x320ce049 0x54495 0x3a23fab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
And here is all of my code starting with the for loop. The error prints to the console as soon as it gets to "if([phoneNumber rangeOfString:#"+"].location == NSNotFound)":
for (i = 0; i < [allContacts count]; i++)
{
Person *person = [[Person alloc] init];
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
NSString *firstName = (__bridge_transfer NSString
*)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
NSString *phoneNumber = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);
person.firstName = firstName;
person.phoneNumber = phoneNumber;
NSLog(#"%#", phoneNumber);
//MOBILE PHONE PSEUDOCODE STARTS HERE
// If the value of phoneNumber contains the text “_$!<Mobile>!$_” , then we want to grab a substring out of it.
if([phoneNumber rangeOfString:#"_$!<Mobile>!$_"].location == NSNotFound) {
NSLog(#"Not a mobile phone number");
} else {
NSLog(#"It is a mobile phone number");
// If the value of phoneNumber contains a “+”, then grab the substring that starts with the + and then the next additional 10 spaces and place the substring in the mobilePhoneNumber object. (Which will grab the + sign and the 10 digit phone number. )
if([phoneNumber rangeOfString:#"+"].location == NSNotFound) {
NSLog(#"Phone number does not start with a +");
} else {
NSLog(#"Phone number does start with a +");
NSString *subString = [phoneNumber substringWithRange: NSMakeRange(0, [phoneNumber rangeOfString: #"+"].location)];
NSLog(#"%#", subString);
}
}
//PFQUERY CODE TESTING!!!
}
}
}
Thanks for the help.

The phone Number is no string . it is defined as a multival as there can be 0..x numbers there.
you can't just treat it as a string . you need to enumerate through the multival\
e.g. to get the first of ANY kind
ABMultiValueRef phoneEntries = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSMutableArray *numbers = [NSMutableArray array)];
for(int i = 0; i < ABMultiValueGetCount(phoneEntries); i++) {
NSString *number = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phoneEntries, i))];
// Do what you want with the number
[numbers addObject:number];
}
CFRelease(phoneEntries);

Related

NSArray unrecognized selector sent to instance on Json

I am new to Objective-C And here I have a Json that I've stored into a NSArray. And I am trying to get the English, Dutch, Portuguese
{
English = (
One,
Two,
Three
);
Dutch = (
Een
);
Portuguese = (
Um,
Dois
}
And here's what I've done:
languageArray = result;
for (i = [languageArray count] - 1; i >= 0; i--)
{
NSString *languageTitle = [languageArray objectAtIndex:i];
NSLog(#"languageTitle %#", languageTitle);
}
And I have this error -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x7f8c68e53610 but my languageArray count is 3. I don't know why I can't get the objectAtIndex:i I've tried to use objectAtIndex:0 but still the same result
just use
NSArray*languages = [result allKeys];
for getting values for languages you have to do this
NSMutableArray *mutableArray = [[NSMutableArray alloc]init];
for (int i=0 ; i<languages.count ; i++){
[mutableArray addObject:[result valueForKey:[languages objectAtIndex:indexpath.row]]];
}
So finally you have mutable array which contains values for every language as array;
You can get values for english with respect to index of language.
For example you can get values for Dutch as
NSArray *dutchValues = [mutableArray objectAtIndex:1];

NSCFNumber escapedString Error with a NSDictionary

So I'm getting this error when creating a NSDictionary:
DLog(#"hiliteID: %# | regionID: %#", hiliteID, regionID);
if ([hiliteID isKindOfClass:[NSNumber class]]) {
DLog(#"hititeID is a number");
}
if ([hiliteID isKindOfClass:[NSString class]]) {
DLog(#"hiliteID is a string");
}
if ([regionID isKindOfClass:[NSNumber class]]) {
DLog(#"regionID is a number");
}
if ([regionID isKindOfClass:[NSString class]]) {
DLog(#"regionID is a string");
}
NSDictionary *regionDictionary = [NSDictionary dictionaryWithObject:regionID forKey:hiliteID];
DLog(#"regionDictionary: %#", regionDictionary);
id result = [self.serverCall XMLRPCCall:kSaveHilitedObjects withObjects:#[self.mapContext, regionDictionary]];
What is logged:
DEBUG | hiliteID: 160399 | regionID: 950
DEBUG | hititeID is a number
DEBUG | regionID is a number
DEBUG | regionDictionary: {
160399 = 950;
}
DEBUG -[__NSCFNumber escapedString]: unrecognized selector sent to instance 0x7947f5d0
DEBUG *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber escapedString]: unrecognized selector sent to instance 0x7947f5d0'
0x7947f5d0 has a value of 160399 so it is hiliteID.
hiliteID is a returned value from our server and is set as a NSString. I cast it to a NSNumber:
NSArray *hiliteIDs = [result allKeys];
if ([[hiliteIDs firstObject] isKindOfClass:[NSString class]]) {
return [NSNumber numberWithInteger:[[hiliteIDs firstObject] integerValue]];
}
else if ([[hiliteIDs firstObject] isKindOfClass:[NSNumber class]]) {
return [hiliteIDs firstObject];
}
As far as I know, there is no issue with what I am doing here.
the line:
id result = [self.serverCall XMLRPCCall:kSaveHilitedObjects withObjects:#[self.mapContext, regionDictionary]];
I've used this class dozens of times in the code and never had an issue.
What can be causing the error?
reason: '-[__NSCFNumber escapedString]: unrecognized selector sent to instance 0x7947f5d0'
Someone trying to call -escapedString method from NSNumber class, so it seems like the problem is that you passing NSNumber argument when NSString required. Try to use only NSString values inside your NSDictionary.
Whatever XMLRPCCall:withObjects: is calling an invalid method. I would recommend making all inputs into this method into NSStrings so that that call doesn't internally call methods on NSNumbers that NSNumber isn't capable of responding to.
NSString *regionIDString = [regionID stringValue];
NSString *hiliteIDString = [hiliteID stringValue];
NSDictionary *regionDictionary = [NSDictionary dictionaryWithObject:regionIDString forKey:hiliteIDString];

iOS app crashing with error: NSCFNumber stringByReplacingOccurrencesOfString:withString: unrecognized selector sent to instance

Our iOS app terminates with an error of [__NSCFNumber stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x1755e090.
We're testing on an iPhone 5 w iOS 7.
It crashes on this line: [self parseDictionary:notificationMessage intoJSON:jsonStr].
Method containing this line:
- (void)notificationReceived {
NSLog(#"Notification received");
if (notificationMessage && self.callback)
{
NSMutableString *jsonStr = [NSMutableString stringWithString:#"{"];
[self parseDictionary:notificationMessage intoJSON:jsonStr];
if (isInline)
{
[jsonStr appendFormat:#"foreground:\"%d\"", 1];
isInline = NO;
}
else
[jsonStr appendFormat:#"foreground:\"%d\"", 0];
[jsonStr appendString:#"}"];
NSLog(#"Msg: %#", jsonStr);
NSString * jsCallBack = [NSString stringWithFormat:#"%#(%#);", self.callback, jsonStr];
[self.webView stringByEvaluatingJavaScriptFromString:jsCallBack];
self.notificationMessage = nil;
}
}
-(void)parseDictionary:(NSDictionary *)inDictionary intoJSON:(NSMutableString *)jsonString
{
NSArray *keys = [inDictionary allKeys];
NSString *key;
for (key in keys)
{
id thisObject = [inDictionary objectForKey:key];
if ([thisObject isKindOfClass:[NSDictionary class]])
[self parseDictionary:thisObject intoJSON:jsonString];
else
[jsonString appendFormat:#"\"%#\":\"%#\",",
key,
[[[[inDictionary objectForKey:key]
stringByReplacingOccurrencesOfString:#"\\" withString:#"\\\\"]
stringByReplacingOccurrencesOfString:#"\"" withString:#"\\\""]
stringByReplacingOccurrencesOfString:#"\n" withString:#"\\n"]];
}
}
Stack trace (along with one debugging line to show the variable value):
2014-01-07 16:32:36.980 Wopple[195:60b] Notification received
Printing description of self->notificationMessage:
{
"_" = "gHf8EeO3_ZDiugJkgA";
aps = {
alert = "hi foo bar right";
badge = 3;
};
}
2014-01-07 16:33:22.774 Wopple[195:60b] -[__NSCFNumber stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x1755e090
2014-01-07 16:33:22.776 Wopple[195:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x1755e090'
*** First throw call stack:
(0x308c3e83 0x3ac246c7 0x308c77b7 0x308c60af 0x30814dc8 0x88da5 0x88d1f 0x88915 0x8757b 0x3335ec93 0x3335f75d 0x340d0b37 0x3088e777 0x3088e713 0x3088cedf 0x307f7471 0x307f7253 0x3552b2eb 0x330ac845 0x7f5d3 0x3b11dab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
When you're using:
[[[[inDictionary objectForKey:key]
stringByReplacingOccurrencesOfString:#"\\" withString:#"\\\\"]
stringByReplacingOccurrencesOfString:#"\"" withString:#"\\\""]
stringByReplacingOccurrencesOfString:#"\n" withString:#"\\n"]];
You're assuming that objectForKey is going to return an NSString object. But, in the case where your app is crashing, the object returned is actually an NSNumber.
You should use isKindOfClass: to determine the object type, or use stringValue on your NSNumber to get a string representation of the object.

Issue when differentiating metadata for UILabel?

I am fetching meta data for the radio streams that currently being played in my iOS app, (Artist Name, track name). However this meta data comes in one piece i.e = "FRANK SINATRA - THEME FROM NEW YORK, NEW YORK".
While this is okay, It would look much nice if I had 2 different labels styled differently one saying "FRANK SINATRA" while the other would read "THEME FROM NEW YORK, NEW YORK".
To do this, I have to get rid of the hyphen,
_metaData = infoString; // THIS IS THE OLD 1- LABEL FOR DATA ALL WAY
// THIS IS THE CODE I AM IMPLEMENTING TO GET RID OF THE HYPHEN
NSUInteger xxx = [_metaData rangeOfString:#"-"].location;
NSUInteger xxxPlustTwo = xxx + 2;
NSString *xxxx = [_metaData substringToIndex:xxx];
self.artistName.text=xxxx;
NSString *trackInformation = [_trackName substringFromIndex:xxxPlustTwo];
self.soundInfoMeta.text = trackInformation;
And just like that I am able to break the 2 apart, problem is that when the station goes to commercial my app immediately crashes. The following is the error I receive on Xcode:
***Terminating app due to uncaught exception ‘NSRangeException’, reason: ‘***-[__NSCFString substringToIndex:]: Index 2147483647 out of bounds; string length 17’
What can be causing this problem?
Update: I tried changing the NSUInteger to double but this did not fix the problem, here is the latest crash log:
xxxx NSString * nil 0x00000000
NSObject NSObject
isa Class 0x0
xxx double 2147483647 2147483647
xplus double 2147483649 2147483649
_trackName __NSCFString * #"http://www.181.fm" 0x16d8a890
NSObject NSObject
isa Class __NSCFString 0x39d9a8f8
You're probably getting input that has no hyphen, so the location of the the hyphen is NSNotFound which is defined to be NSIntegerMax, I believe. Your code needs to be robust to this.
This is a bit different from how you're doing it now, but it should work on more types of input:
NSString *infoString = #"ARTIST - TRACK";
NSArray *infoStringComponents = [infoString componentsSeparatedByString:#" - "];
__block NSString *artistString = #"";
__block NSString *trackInfoString = #"";
if ([infoStringComponents count] == 0) {
// This case should only happen when there's no info string at all
artistString = #"Unknown Artist";
trackInfoString = #"Unknown Song";
}
else if ([infoStringComponents count] == 1) {
// If no hyphens just display the whole string as the track info
trackInfoString = infoStringComponents[0];
}
else {
[infoStringComponents enumerateObjectsUsingBlock:^(NSString *component, NSUInteger index, BOOL *stop) {
NSString *trimmedComponent = [component stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (index == 0) {
artistString = trimmedComponent;
}
else {
NSString *buffer = #"";
if ([trackInfoString length] > 0) {
buffer = #" - ";
}
trackInfoString = [trackInfoString stringByAppendingFormat:#"%#%#",buffer,trimmedComponent];
}
}];
}
You could also check if the location property of the range you derive is equal to NSNotFound and then just assume you can't derive an artist out of it and display your _metaData variable in an appropriate label. For example:
NSRange hyphenRange = [infoString rangeOfString:#"-"];
if (hyphenRange.location == NSNotFound) {
// Display only infoString to the user, unformatted into artist / song info
}
else {
// Try the same technique you're attempting now
}

NSDecimalNumbers in Array Causing "Unrecognized Selector..." messages

Ive been playing around with obj-c for a few months now and feel comfortable with a lot of things but cant make sense of something that seems very simple. I need to be able to go from an NSString split into an array then get the NSDecimalNumbers from that array. If I try to perform any calculations, I get an "unrecognized selector sent to instance." If I simply print each decimal number everything goes smoothly. If I put the values manually into NSDecimalNumbers rather than pulling from the array that also works. The error and code are below.
2013-10-14 15:54:42.174 test[30829:c07] -[__NSCFString
decimalNumberByAdding:]: unrecognized selector sent to instance
0x75a81f0 2013-10-14 15:54:42.176 test[30829:c07] * Terminating app
due to uncaught exception 'NSInvalidArgumentException', reason:
'-[__NSCFString decimalNumberByAdding:]: unrecognized selector sent to
instance 0x75a81f0'
* First throw call stack: (0x1c90012 0x10cde7e 0x1d1b4bd 0x1c7fbbc 0x1c7f94e 0x2075 0x10e1705 0x152c0 0x15258 0xd6021 0xd657f 0xd56e8
0x44cef 0x44f02 0x22d4a 0x14698 0x1bebdf9 0x1bebad0 0x1c05bf5
0x1c05962 0x1c36bb6 0x1c35f44 0x1c35e1b 0x1bea7e3 0x1bea668 0x11ffc
0x1bfd 0x1b25) libc++abi.dylib: terminate called throwing an exception
-(IBAction)buttonPushed: (id) sender
{
NSString* s = #"25.55, 109.24";
NSArray* a = [s componentsSeparatedByString: #" "];
NSDecimalNumber* dec1 = [a objectAtIndex: 0];
NSDecimalNumber* dec2 = [a objectAtIndex: 1];
NSDecimalNumber* sum = [dec1 decimalNumberByAdding: dec2];
statusText.text = [NSString stringWithFormat: #"%#", sum, nil];
}
NSDecimalNumber* dec1 = [a objectAtIndex: 0];
doesn't give you a NSDecimalNumber object. It gives you a string (which was created via the call to componentsSeparatedByString)
Try doing this:
NSDecimalNumber* dec1 = [NSDecimalNumber decimalNumberWithString: [a objectAtIndex: 0]];
and see if you have better luck.
you never created a NSDecimalNumber object
NSArray* a = [s componentsSeparatedByString: #", "];
will of course create an aray of strings.
Just because you type the pointer as decimal number, doesnt make the obejct to be one
NSDecimalNumber* dec1 = [a objectAtIndex: 0];
dec1 is still a NSString. Try
NSDecimalNumber* dec1 = [NSDecimalNumber decimalNumberWithString:[a objectAtIndex: 0]];
NSDecimalNumber* dec2 = [NSDecimalNumber decimalNumberWithString:[a objectAtIndex: 1]];
NSDecimalNumber* sum = [dec1 decimalNumberByAdding: dec2];
statusText.text = [NSString stringWithFormat: #"%#", sum];
When you call [a objectAtIndex: 0] that is just returning a string, that you're trying to cast to a NSDecimalNumber.
Instead, try using something like this:
-(IBAction)buttonPushed: (id) sender
{
NSString* s = #"25.55, 109.24";
NSArray* a = [s componentsSeparatedByString: #", "];
NSDecimalNumber* dec1 = [NSDecimalNumber decimalNumberWithString:[a objectAtIndex: 0]];
NSDecimalNumber* dec2 = [NSDecimalNumber decimalNumberWithString:[a objectAtIndex: 1]];
NSDecimalNumber* sum = [dec1 decimalNumberByAdding: dec2];
statusText.text = [NSString stringWithFormat: #"%#", sum, nil];
}
Also note that I'm splitting the string with ", " and not just " ", otherwise the first string will be "25.55,"
See the NSDecimalNumber documentation for the details: https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/classes/NSDecimalNumber_Class/Reference/Reference.html#//apple_ref/occ/clm/NSDecimalNumber/decimalNumberWithString:

Resources