Objective C: Error when trying to initialize instance of CLLocation - objective-c

I am hitting an error (program crashed) when it hits the following code
CLLocation *userLoc = [[CLLocation alloc] initWithCoordinate:locationManager.location.coordinate];
I tried to replace the argument with the following and I get the same error as well
CLLocation *userLoc = [[CLLocation alloc] initWithCoordinate:mapView.userLocation.coordinate];
Can anyone advise me on what should be the correct argument to use here for the user's current location?
Thanks!
Zhen
Stacktrace:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CLLocation initWithCoordinate:]: unrecognized selector sent to instance 0x1e0620'

According to the documentation, CLLocation does not have an initializer called initWithCoordinate:. Use one of the following:
initWithLatitude:longitude:
initWithCoordinate:altitude:horizontalAccuracy:verticalAccuracy:timestamp:
initWithCoordinate:altitude:horizontalAccuracy:verticalAccuracy:course:speed:timestamp:

Related

Is there anyway to check if my app calls any method exclusive for iOS 8?

After testing my app in a iOS 7 device, I got a crash calling containsString from NSString, and then I realised that that methods has NS_AVAILABLE(10_10, 8_0);
Is there anyway to check automatically my program to see if there is any other method that is not available in iOS 7?
How is possible to compile and app with deployment target of 7.0 and not saying any warning or error in these cases?
-[__NSCFString containsString:]: unrecognized selector sent to instance 0x1702297c0
2015-10-23 12:03:37.655 WA[1434:60b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString containsString:]: unrecognized selector sent to instance 0x1702297c0'
* First throw call stack:
(0x183bd2f50 0x1900dc1fc 0x183bd7c04 0x183bd5930 0x183af55dc 0x1002a2c08 0x1000c71dc 0x1000caa88 0x1000c69bc 0x1000f3a4c 0x186c1055c 0x186c0ff08 0x186c099ec 0x186b9d8cc 0x186b9cad0 0x186c09044 0x1897bb504 0x1897bb030 0x183b92e90 0x183b92df0 0x183b91014 0x183ad1c20 0x186c081c8 0x186c02fdc 0x1000b6040 0x1906cfaa0)
libc++abi.dylib: terminating with uncaught exception of type NSException
You should use rangeOfString instead of containsString. Since rangeOfString available from iOS 2.0, you will not get crash on this.
Code snippets,
NSString *string = #"https://www.google.co.in/";
NSString *substring = #"http";
if ([oneContent rangeOfString:subString].location == NSNotFound) {
NSLog(#"string does not contain substring");
} else {
NSLog(#"string contains substring!");
}
Otherwise you can add category class for NSString and have a method like containsString: where you can implement the above code. It'll work dynamically.

trouble debugging during runtime

I have written a some code for a drill down table app I have been making, but the app crashes only during runtime. Xcode doesn't give me any errors while building the app. The debugger outputs:
2012-10-18 10:58:26.513 second[474:c07] -[NavController setItems:]: unrecognized selector >sent to instance 0xc217a00
2012-10-18 10:58:26.515 second[474:c07] * Terminating app due to uncaught exception >'NSInvalidArgumentException', reason: '-[NavController setItems:]: unrecognized selector sent >to instance 0xc217a00'
* First throw call stack:
(0x14b8022 0xeb8cd6 0x14b9cbd 0x141eed0 0x141ecb2 0x3fbe 0xe2a1e 0x41401 0x41670 0x41836 >0xbfc9dd8 0x4872a 0x19596 0x1a274 0x29183 0x29c38 0x1d634 0x13a2ef5 0x148c195 0x13f0ff2 >0x13ef8da 0x13eed84 0x13eec9b 0x19c65 0x1b626 0x1d40 0x1cd9)
terminate called throwing an exception
I think I understand that the error lies in NavController.m where:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString* path = [[NSBundle mainBundle] pathForResource:#"Data" ofType:#"plist"];
MasterViewController* root = (MasterViewController*)self.topViewController;
NSDictionary* thelist = [NSDictionary dictionaryWithContentsOfFile:path];
root.items = [thelist objectForKey:#"Items"];
root.navigationItem.title = [thelist objectForKey:#"name"];
}
btw, I made the array items like this: (nonatomic, retain) NSArray* items;
The app crashes, because of the following line:
root.items = [thelist objectForKey:#"Items"];
This line is just a shortcut for writing:
[root setItems:[thelist objectForKey:#"Items"]];
And the runtime complains that there is no method setItems: found. The reason why there is compile error is because you tell the compiler that root is of class MasterViewController, however, that is not true. At runtime the obj-c runtime finds out that root is in fact of class NavController and it seems like NavController has no setItems: method.
In other words, this line is wrong:
MasterViewController* root = (MasterViewController*)self.topViewController;
You lie about the class; self.topViewController returns an object that is of class NavController, yet you force the compiler to treat it as MasterViewController which will fail as soon as you call a method that is not found for NavController.
The problem is probably here:
MasterViewController* root = (MasterViewController*)self.topViewController;
self.topViewController is proably not an instance of MasterViewController, but a NavController instance and there you go.

how to solve run time error of [NSURL fileURLWithPath:error:]: unrecognized selector sent to class?

I am beginner in IPhone Developing. I want play sound. so, that's why I have apply this code
(void)viewDidLoad
{
NSError* err;
path=[[NSBundle mainBundle] pathForResource:#"Animalfile" ofType:#"plist"];
dict=[NSDictionary dictionaryWithContentsOfFile:path];
NSArray *animalaudio=[dict valueForKey:#"audio"];
NSString *audiolist=[animalaudio objectAtIndex:currentsound];
AVAudioPlayer *audio=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:audiolist error:&err]];
audio.delegate=self;
[audio play];
}
and I have got the run time error of
[NSURL fileURLWithPath:error:]: unrecognized selector sent to class 0x182121c
2012-07-14 14:51:21.711 plistdemo[1236:10703] *** Terminating app due to uncaught
exception 'NSInvalidArgumentException', reason: '+[NSURL fileURLWithPath:error:]:
unrecognized selector sent to class 0x182121c'
terminate called throwing an exceptionsharedlibrary apply-load-rules all
so, give any suggestion and source code which apply in my code
current documentation for NSURL doesn't list any such method like 'fileURLWithPath:error:'... seems like its either deprecated or incorrect.
instead try using something like:
[NSURL fileURLWithPath:audioList]
hope it helps...

Error saving to iOS keychain using KeychainItemWrapper

I get an error every time I try to store data into the keychain
The error comes from the dictionaryToSecItemFormat method at these lines
NSString *passwordString = [dictionaryToConvert objectForKey:(id)kSecValueData];
[returnDictionary setObject:[passwordString dataUsingEncoding:NSUTF8StringEncoding] forKey:(id)kSecValueData]'
The error is
I'm calling the KeychainItemWrapper methods like this
KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:#"credentials" accessGroup:nil];
[keychain setObject:username forKey:kSecAttrAccount];
[keychain release];
The error is
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'-[NSConcreteData dataUsingEncoding:]: unrecognized selector sent to instance"
dataUsingEncoding is a method from the NSString class, make username a NSString.

set file's icon in a command line utility not working

I just began to work with Objective-C and I'm managing pretty well. My last challenge was to make a command line utility, which I could than use in AppleScript. But my code does not work, not in the terminal, not in the AppleScript. So I'm asking you, what's the error in this peace of code, that should be very plain and easy?
int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// -imagePath
// -filePath
NSUserDefaults *args = [NSUserDefaults standardUserDefaults];
NSString *soundPath = [args stringForKey:#"imagePath"];
NSString *filePath = [args stringForKey:#"filePath"];
BOOL worked = [[NSWorkspace sharedWorkspace] setIcon:[[NSImage alloc] initWithContentsOfFile:soundPath] forFile:filePath options:0];
NSLog(#"Worked: %i",worked);
[pool release];
return 0;
}
2010-01-31 17:03:24.317 iConChange[14848:10b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error (1002) creating CGSWindow'
In effect, that means “You can't do that in a command-line tool”. If you run your tool in the debugger, it'll tell you what “that” is in the stack trace. My guess is that “that” is creating the NSImage.
Another solution is to rewrite the tool to use Icon Services instead of NSWorkspace. The APIs you'll need are still available and not deprecated.
Terminal:
macbook-van-ief2:~ ief2$ /Users/ief2/Desktop/iConChange/build/Debug/iConChange -filePath "~/Desktop/Naamloos.txt" -imagePath "~/Desktop/Z_Home_ZIcon.gif"
2010-01-31 17:03:24.311 iConChange[14848:10b] _NXCreateWindow: error setting window property (1002)
2010-01-31 17:03:24.317 iConChange[14848:10b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error (1002) creating CGSWindow'
2010-01-31 17:03:24.322 iConChange[14848:10b] Stack: (
2459177131,
2487344699,
2459176587,
2459176650,
2441787103,
2441786331,
2441785537,
2441784212,
2441781861,
2441794711,
2441793509,
2441762807,
2444980701,
2444978472,
2447881218
)
Trace/BPT trap
2010-01-31 17:03:24.317 iConChange[14848:10b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error (1002) creating CGSWindow'
In effect, that means “You can't do that in a command-line tool”. If you run your tool in the debugger, it'll tell you what “that” is in the stack trace.
One solution is to rewrite it as an application. Perhaps a document-based one, with an image view in the document window, set up to receive dragged images and files.
I have an application that uses basically that code to set icons on OS X:
http://pzich.com/junk/Iconizer.app.zip
I'm not sure why yours isn't working.