Use of undeclared identifier [objective-c] - objective-c

i have an Object_Info.h (contains the interface declaration) and Object_Info.m (contains some not all method implementations) files in the same directory as my main.m.
in main.m
#import <Foundation/Foundation.h>
#import "Object_Info.h"
int main(int argc, const char * argv[]) {
#autoreleasepool {
error thrown here -> use of undeclared identifier 'tempObjInfo'
(Object_Info*) tempObjInfo = [[Object_Info alloc] init];
}
return 0;
}
any ideas as to why the error is there? user of undeclared identifier 'tempObjInfo'
I've tried (Object_Info*) tempObjInfo = [Object_Info new] as well with no success.
Thanks!

You didn't declare the identifier. You should declare it like...
Object_Info *tempObjInfo = [[Object_Info alloc] init];
... or if the tempObjInfo symbol is declared somewhere globally you will need to include the header where it is being declared.

Related

Newbie Objective C Error such as _main

I've been tinkering all day, and I can't seem to fix this error.
Here's the code:
//
// main.m
// Learning ObjC
//
// Created by Nickirv on 8/9/15.
// Copyright (c) 2015 Nickirv. All rights reserved.
//
#import <Foundation/Foundation.h>
#interface Person: NSObject{
int age;
int weight;
}
-(void) print;
-(void) setAge: (int) a;
-(void) SetWeight: (int) w;
#end
And it outputs this issue:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I would appreciate any help! Thank you very much!
You can't delete the main boilerplate code:
int main(int argc, const char * argv[]) {
#autoreleasepool {
}
return 0;
}
In the end Objective-C is "C" and the program starts execution by calling main. Additionally Objective-C code needs to execute in an autoreleasepool.
You define class #interfaces and #implemtations outside of (generally above) the boilerplate but the first line of code to run must be within the autoreleasepool scope {}.
Here is an example Objective-C program similar to what the OP seems to want using #properties for simplicity and demonstration.
It is important to study Objective-C documentation until the following code is fully understood, td;dr does not work for this.
#import <Foundation/Foundation.h>
#interface Person: NSObject
#property int age;
#property int weight;
- (void)print;
#end
#implementation Person : NSObject
- (void)print {
printf("Age: %i, weight: %i", self.age, self.weight);
}
#end
int main(int argc, const char * argv[]) {
#autoreleasepool {
Person *don = [[Person alloc] init];
don.weight = 130;
don.age = 23;
[don print];
}
return 0;
}
Output:
Age: 23, weight: 130

Trouble with calling a method in Objective C (Apple Documentation example)

I'm following along with Apple's "Programming with Objective C" document, the link being: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithObjects/WorkingwithObjects.html#//apple_ref/doc/uid/TP40011210-CH4-SW1
Anyways, I've gotten to the point where it ask for calling the sayHello method.
"Create a new XYZPerson instance using alloc and init, and then call the sayHello method."
#import <Foundation/Foundation.h>
#import "XYZPerson.h"
int main(int argc, const char * argv[]);
XYZPerson *firstPerson = [[XYZPerson alloc] init]; //Initializer element is not a lime-time constant
[firstPerson sayHello]; //No Visible #interface for 'XYZPerson' delcares the selector 'sayHello'
#implementation XYZPerson
- (void)sayHello {
[self saySomething:#"Hello, World"];
}
- (void)saySomething: (NSString *)greeting {
NSLog(#"%#", greeting);
}
#end
I believe I'm having a misunderstanding with how apple is explaining the work or just have no clue.
Wishing apple had these examples done for us to review over.
You need to put the code inside the main function. Right now you have the code just sitting in your file, outside of any function. It should be:
int main(int argc, const char * argv[]) {
XYZPerson *firstPerson = [[XYZPerson alloc] init];
[firstPerson sayHello];
}
Also, according to the docs you should have a separate main.m file that has your main function inside of it.
As you can only access public functions which are declared in .h file with the class object.
Kindly declare that function in .h file and it will solve your problem

How do I properly declare a global variable in a header file?

I was testing some code where I declare a global variable in a header file, but I'm getting a linker error: "duplicate symbol"
header file:
//
// GlobalVaraibleClass.h
// GlobalVar
//
#import <Foundation/Foundation.h>
int gGlobalVar = 0;
#interface GlobalVaraibleClass : NSObject
#end
class file:
//
// GlobalVaraibleClass.m
// GlobalVar
//
#import "GlobalVaraibleClass.h"
#implementation GlobalVaraibleClass
#end
main:
//
// main.m
// GlobalVar
//
#import <Foundation/Foundation.h>
#import "GlobalVaraibleClass.h"
int main(int argc, const char * argv[])
{
#autoreleasepool {
extern int gGlobalVar;
NSLog(#"Hello, World! %i", gGlobalVar);
}
return 0;
}
where am I going wrong?
That is backwards, the extern goes in the header, the declaration setting the value goes in the implementation file.
The extern specifies that the variable will be declared somewhere else. If the declaration is in the header every time the header is included there will be another declaration and at link time there will be multiple definitions which will not link.
Example:
// GlobalVaraibleClass.h
extern int gGlobalVar;
// GlobalVaraible.m
#import "GlobalVaraibleClass.h"
int gGlobalVar = 3;
// main.m
#import <Foundation/Foundation.h>
#import "GlobalVaraibleClass.h"
int main(int argc, const char * argv[]) {
#autoreleasepool {
NSLog(#"Hello, World! %i", gGlobalVar);
}
return 0;
}

Unable to instantiate the UIApplication subclass instance

i'm building a SpriteBuilder project and getting the error of:
" * Assertion failure in int UIApplicationMain(int, char **, NSString *, NSString *)(), /SourceCache/UIKit_Sim/UIKit-2903.23/UIApplication.m:2380
Unable to instantiate the UIApplication subclass instance. No class named NSApplication is loaded."
For the main.m code of:
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
#autoreleasepool {
int retVal = UIApplicationMain(argc, argv, nil, #"AppController");
return retVal;
}
}
What's the cause?
XCode is referring to the wrong info.plist file. In Build Settings, the info.plist path should be "Source/Resources/Info.plist" instead of "$(SRCROOT)/Source/libs/cocos2d-iphone/external/Chipmunk/xcode/main-Info.plist". Changing the path fixed it.

Simple objective-c code segfaulting with NSWindow windowNumbersWithOptions

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
int main(int argc, const char * argv[])
{
#autoreleasepool {
NSArray* windowList = [[NSArray alloc] init];
windowList = [NSWindow windowNumbersWithOptions:0];
NSLog(#"%lu", [windowList count]);
}
return 0;
}
Segfaulting when calling NSWindow windowNumbersWithOptions
The doc for windowNumbersWithOptions says, for the option value,
If the value 0 is passed instead, then the list returned from the method contains window numbers for visible windows on the active space belonging to the calling application.
Probably your windowList is NULL, because your app didn't create one?