Before I run this program I have 0 errors or warnings. But when I run it the output I am getting is (lldb) and XCode highlights my NSLog output and says Thread 1 Breakpoint 1? My first question is why am I not getting any output, and how can I fix what I have to get output? And my second question is how can I break up each one of these parts and put them into their own class. So not have them all in main.m. This is my first day coding in XCode coming from Java so I’m still learning this language. Thank you.
Part 1
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
NSString * name;
int age;
int weight;
}
/***************************************
* MUTATORS FOR PERSON INTERFACE
***************************************/
- (void) setName : (NSString *) n;
- (void) setAge : (int) a;
- (void) setWeight : (int) w;
/***************************************
* MUTATORS FOR PERSON INTERFACE
***************************************/
-(NSString *) getName;
-(int) getAge;
- (int) getWeight;
@end
Part 2
@implementation Person
/***************************************
* MUTATORS FOR PERSON IMPLEMENTATION
***************************************/
-(void) setName:(NSString *) n
{
name = n;
}
-(void) setAge:(int) a
{
age = a;
}
-(void) setWeight:(int)w
{
weight = w;
}
/**********************************
* ACCESSORS FOR PERSON IMPLEMENTATION
**********************************/
-(NSString *) getName
{
return name;
}
-(int) getAge
{
return age;
}
-(int) getWeight
{
return weight;
}
@end
Part 3 This is where I have the problem. It doesen’t like NSLog
int main (int argc, const char * argv[])
{
@autoreleasepool
{
Person *p1 = [[Person alloc]init];
[p1 setName: @"Chris"];
[p1 setAge:18];
[p1 setWeight:200];
NSLog(@"\nName: %@ \nAge: %i \nWeight: %i", p1.getName, p1.getAge, p1.getWeight);
}
return 0;
}
When using NSLog,
%@is used for logging objects, but those methods return int (a primitive type). Replace them with%d(string formatting for Decimal int) and that line should stop crashing.To split them up, simply create new .m and .h files. XCode should be able to help you with this process. The .m files will automatically be compiled, and .h files should (generally speaking) be included by any file that needs information about the class it refers to. So Part 1 would be Person.h, Part 2 would be Person.h (and include the line
@import Person.h, and Part 3 would remain in main.m.