Use of Undeclared Identifier error in CoreLocation - location

I don't under why the error: Use of undeclared identifier 'KCLDistanceFilterNone' and 'kCLLocationAccuracyHundredMeters' keeps coming up for my CoreLocation.m folder. There are also many more errors even when I deleted the ";" from select lines. Can someone please help?
#import "CoreLocation.h"
#implementation CoreLocation : NSObject
- (NSString *)deviceLocation {
NSString *theLocation = [NSString stringWithFormat:#"latitude: %f longitude: %f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude];
return theLocation;
}
- (void)viewDidLoad
{
locationManager = [[locationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
}
#end
Also, what would I put in my CoreLocation.m folder to complement this and complete the location services for my application?

A couple of thoughts:
Have you included the CoreLocation.framework in the list of linked libraries/frameworks?
Have you done the import of CoreLocation.framework header?
#import <CoreLocation/CoreLocation.h>
I wouldn't have thought that CoreLocation is a good name for your class, because of the confusion between the above line, and your line that says:
#import "CoreLocation.h"
It should work (having both of those lines), but it seems unnecessarily confusing (and risks problems associated with #import's feature that automatically prevents loading the same .h file twice).
I'd suggest give your custom CoreLocation class (a) a unique name that doesn't risk confusion with the existing headers; and (b) a more meaningful name that indicates what it's doing (e.g. CoreLocationUtilities or AppCoreLocationManager or whatever).
Your alloc/init method line references a variable name:
locationManager = [[locationManager alloc] init];
It should presumably reference a class name, e.g.:
locationManager = [[CLLocationManager alloc] init];
It makes me wonder how you defined the locationManager instance variable if you didn't get an error on that line.
Unrelated to your issue, I'm confused by your reference of your custom CoreLocation class being a NSObject subclass, but then having a viewDidLoad method, which is typically a view controller method. That seems to only further muddy the waters.

Related

How to send a message to the Master Class of a class?

Quick Question:
I am creating an object from the MainWindowController:
about = [[About alloc]init];
In the Class About I do my Init:
-(id)init{
if(!_viewAbout){
[NSBundle loadNibNamed:#"About" owner:self];
[NSApp beginSheet:self.viewAbout modalForWindow:*?????* modalDelegate:self didEndSelector:NULL contextInfo:NULL];
}
return self;
}
My problem is that the Window is created in the MainWindowController. My question is how to call/send a message to the creator of the class if the class itself doesn't know the master class?
If I understand you correctly, most classes have self.superclass and just super, like
[super someMethod....
or
[self.superclass blegh....
Or are you asking for the class that creates another class ? If that is the case, you need to declare the creator class inside the other one, some (id) variable would do the trick.
But the most popular design pattern on the mac is the delegate pattern, and once you start using that you will love it. Declaring a delegate is usually the way Cocoa and UIKit do things, but other programming languages might not. Obj-C doesn't have any magic variables like python f.ex. has. Either you have a delegate or you have a declared variable which you would set right after the init/alloc stuff.
Also your (init) call doesn't look right. Usually it looks like :
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Here you could declare your setting.
}
return self;
}
But my opinion is that if you are declaring a singular pattern, you would call a specific method in your class, like you do with so many classes on the iOS/Cocoa, like :
[someclass DefaultClass]
This would be your init class where you would do init, unless the class had been declared before and then you would just return the object.

NSKeyedArchiver: distinguishing between different instances of the same class

I'm implementing support for Lion's "Resume" feature in my OS X app.
I have a custom subclass of NSViewController in which I implemented the method
encodeRestorableStateWithCoder: as:
#implementation MyClass (Restoration)
-(void)encodeRestorableStateWithCoder:(NSCoder*)coder {
[coder encodeObject:_dataMember forKey:#"object_key"]; // I get the warning below when this line is executed for the second time
}
- (void)restoreStateWithCoder:(NSCoder *)coder {
_dataMember = [coder decodeObjectForKey:#"object_key"];
}
#end
However, since I have multiple instances of MyClass, different values are saved into the same key ("object_key") and I get the following warning from Cocoa:
NSKeyedArchiver warning: replacing existing value for key
'object_key'; probable duplication of encoding keys in class hierarchy
What is the best practice to overcome this problem?
Edit: I found here that each instance automatically has its own namespace to avoid collisions, so the problem might be in the way I'm manually calling encodeRestorableStateWithCoder to different instances with the same NSCoder object without telling it that these are different instances. However, I still can't figure out how to do that properly.
Thanks in advance!
To overcome this problem, it is possible to create a new NSMutableData where each of which is written by a separate (new) NSKeyArchiver, and store them all in an array which is stored in the original NSCoder object.
Here is an example for encoding the restorable state of subitems. The decoding part can be straight-forward given this code.
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
[super encodeRestorableStateWithCoder:coder];
// Encode subitems states:
NSArray* subitems = self.items;
NSMutableArray* states = [NSMutableArray arrayWithCapacity: subitems.count];
for (SubItemClass* item in subitems)
{
NSMutableData* state = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:state];
[item encodeRestorableStateWithCoder:archiver];
[archiver finishEncoding];
[states addObject:state];
}
[coder encodeObject:states forKey:#"subitems"];
}

XCode- Need Help With Errors (Expected ; and Expected Statement)

.m coding:
-(void)viewDidLoad {
NSString *path = [[NSBundle mainBundle] pathForResource:#"MathMusic2" ofType:#"wav"];
self.theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]
autorelease]; //error: expected ';' before 'autorelease' and // error: expected statement before ']' token
theAudio.delegate = self;
[theAudio play];
theAudio.numberOfLoops = -1;
}
related warnings:
warning: property 'theAudio' requires
method '-theAudio' to be defined - use
#synthesize, #dynamic or provide a
method implementation
warning: property 'theAudio' requires
the method 'setTheAudio:' to be
defined - use #synthesize, #dynamic or
provide a method implementation
tell me if you need .h coding. But there are no errors there.
The two errors are because you are missing an opening [ in the previous line:
self.theAudio = [[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL] autorelease];
The warnings are because you forgot #synthesize theAudio; in your #implementation (or forgot to write custom getter and setter methods). At runtime, you'll get an unknown selector exception if you don't fix that.
It was me in the previous question, my code was erroneous, this should correct it:
self.theAudio = [[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL] autorelease];
To this error:
warning: property 'theAudio' requires
method '-theAudio' to be defined - use
#synthesize, #dynamic or provide a
method implementation
Do you know what a property is? If not, look at this short tutorial. I actually provided the code in your previous question, but you have to know where to put it.

How to add a method to an existing protocol in Cocoa?

I want to extend or add another method to an existing protocol. Although the protocol in particular is not important, this is what I am trying to do.
#protocol NSMatrixDelegate
- (void)myNewMethod:(id)sender;
#end
The compiler warns that I have a duplicate declaration of the same protocol. How would I do this properly?
Thanks.
You can't define categories for protocols. There are 2 ways around this:
use a new formal protocol
use an informal protocol and runtime checking
Formal Protocol
Defining a new formal protocol would look like this:
#protocol MyCustomMatrixDelegate <NSMatrixDelegate>
- (void) myNewMethod:(id)sender;
#end
Then you would make your custom class conform to <MyCustomMatrixDelegate> instead of <NSMatrixDelegate>. If you use this approach, there's something to be aware of: [self delegate] will likely be declared as id<NSMatrixDelegate>. This means that you can't do [[self delegate] myNewMethod:obj], because <NSMatrixDelegate> does not declare the myNewMethod: method.
The way around this is to retype the delegate object via casting. Maybe something like:
- (id<MyCustomMatrixDelegate>) customDelegate {
return (id<MyCustomMatrixDelegate>)[self delegate];
}
(However, you might want to do some type checking first, like:
if ([[self delegate] conformsToProtocol:#protocol(MyCustomMatrixDelegate)]) {
return (id<MyCustomMatrixDelegate>)[self delegate];
}
return nil;
)
And then you'd do:
[[self customDelegate] myNewMethod:obj];
Informal Protocol
This is really a fancy name for a category on NSObject:
#interface NSObject (MyCustomMatrixDelegate)
- (void) myNewMethod:(id)sender;
#end
Then you just don't implement the method. In your class that would send the method, you'd do:
if ([[self delegate] respondsToSelector:#selector(myNewMethod:)]) {
[[self delegate] myNewMethod:someSenderValue];
}

XCode, error: '_object' undeclared. Need some help solving this problem

I've got this code in my viewController.m file
- (void)viewDidLoad {
[super viewDidLoad];
GameLogic *_game = [[GameLogic alloc] init];
[_game initGame];
.......
}
GameLogic is another class which I created.
in the same viewController.m file, I have got another function
- (void)test {
if([_game returnElecFence]) //[_game returnsElecFence] causes the error
{
NSLog(#"YES");
}
else {
NSLog(#"NO");
}
.......
}
Problem is, whenever the test function is called, I get an error saying '_game' undeclared. I tried putting the GameLogic init code in the .h file and on top of the #implementation to make it global but every method I tried resulted in a worse error. TIA to anyone who can suggest some ideas to clear this error up
_game is a local variable. Its scope is only the method in which it's declared (viewDidLoad in this case).
You need to make _game a global variable, or better yet, an instance variable of your viewController class so that it can be accessed by all methods of the class.

Resources