What's wrong with my Objective-C class? - objective-c

I am having trouble with my Objective-C code. I am trying to print out all of the details of my object created from my "Person" class, but the first and last names are not coming through in the NSLog method. They are replaced by spaces.
Person.h: http://pastebin.com/mzWurkUL
Person.m: http://pastebin.com/JNSi39aw
This is my main source file:
#import <Foundation/Foundation.h>
#import "Person.h"
int main (int argc, const char * argv[])
{
Person *bobby = [[Person alloc] init];
[bobby setFirstName:#"Bobby"];
[bobby setLastName:#"Flay"];
[bobby setAge:34];
[bobby setWeight:169];
NSLog(#"%s %s is %d years old and weighs %d pounds.",
[bobby first_name],
[bobby last_name],
[bobby age],
[bobby weight]);
return 0;
}

%s is for C style strings (a sequence of chars terminated by a null).
Use %# for NSString objects. In general, %# will invoke the description instance method of any Objective C object. In the case of NSString, this is the string itself.
See String Format Specifiers.
On an unrelated note, you should look into Declared Properties and #synthesize for your class implementation. It will save you a lot of typing as it produces all the getters and setters for you:
person.h:
#import <Cocoa/Cocoa.h>
#interface Person : NSObject
#property (nonatomic, copy) NSString *first_name, *last_name;
#property (nonatomic, strong) NSNumber *age, *weight;
#end
person.m
#import "Person.h"
#implementation Person
#synthesize first_name = _first_name, last_name = _last_name;
#synthesize age = _age, weight = _weight;
#end
main.m
#import <Foundation/Foundation.h>
#import "Person.h"
int main (int argc, const char * argv[])
{
Person *bobby = [[Person alloc] init];
bobby.first_name = #"Bobby";
bobby.last_name = #"Flay";
bobby.age = [NSNumber numberWithInt:34]; // older Objective C compilers.
// New-ish llvm feature, see http://clang.llvm.org/docs/ObjectiveCLiterals.html
// bobby.age = #34;
bobby.weight = [NSNumber numberWithInt:164];
NSLog(#"%# %# is %# years old and weighs %# pounds.",
bobby.first_name, bobby.last_name,
bobby.age, bobby.weight);
return 0;
}

Related

How to input new instance of Object with multiple arguments into NSMutablearray

I'm very new to Obj-C, been learning more Java and C++ lately.
I have two objects Friend and Foe which inherit the Character Object. Friend and Foe have slightly different attributes. I want all Friends and Foes to be in the same NSMutablearray. Can't figure out how to put these into the array. I get an error saying too many arguments, expected 1 have 4. For Foe its the same, but expected 1, have 5.
The Character Object
#import <foundation/foundation.h>
#interface Character : NSObject
#property NSString *name;
#property NSInteger strength;
#property NSInteger iff;
- (void) printDetails;
#end
#import <Foundation/Foundation.h>
#import "game_character.h"
#implementation Character
- (void) printDetails
{
NSLog (#"%# has strength %ld\n", self.name, self.strength);
}
#end
The Friend Object (The Foe object is similar with without intelligence and spell but has an alternate NSInteger attribute.
#interface Friend : Character
#property NSInteger intelligence;
#property NSString *spell;
- (void)printDetails;
#end
#import <Foundation/Foundation.h>
#import "game_character.h"
#import "friend.h"
#implementation Friend
-(void)printDetails
{
NSLog (#"%# has strength %ld\n", self.name, self.strength);
NSLog (#" ,Intelligence %ld, Spell %#\n", self.intelligence, self.spell);
}
#end
The Friend Input Method (I will have a similar method to input a Foe)
void input_friend()
{
#autoreleasepool
{
char str[30] = {0};
NSInteger strength;
NSInteger iff=1;
NSInteger intelligence;
NSLog(#"Enter character name\n");
scanf("%s", str);
NSString *name = [NSString stringWithUTF8String:str];
NSLog(#"Enter character strength\n");
scanf("%ld", &strength);
NSLog(#"Enter character intelligence");
scanf("%ld", &intelligence);
NSLog(#"Enter character spell\n");
scanf("%s", str);
NSString *spell = [NSString stringWithUTF8String:str];
My Error is here when I try to add the object to the array.
[characters addObject:name, strength, iff, intelligence, spell];
}
}
The Main so far. I intend to add a menu with option to add Friend or Foe to the array.
int main(int argc, const char * argv[])
{
#autoreleasepool
{
characters = [[NSMutableArray alloc] init];
void input_friend();
void input_foe();
}
return 0;
}
In this line, you are passing multiple arguments to add to your list of characters. However, this doesn't work since you want to add objects, which is why you got your error:
[characters addObject:name, strength, iff, intelligence, spell];
So you need to initialize a new Friend or Foe first, set its properties, and then add it to your array.
Friend *newFriend = [[Friend alloc] init];
newFriend.name = name;
newFriend.strength = strength;
// etc.
[characters addObject: newFriend];

Property issue in Objective C, 'score' not found on object of type pointer

I just started out programming in Objective C. This is very simple stuff but im wondering what im missing on my knowledge of properties, as far as I understand this should work but it gives me the error:
Property 'score' not found on object of type 'Player *'
I have a Player class and the code as follows below, each bolded is a separate file
Player.h has:
#import <Foundation/Foundation.h>
#interface Player : NSObject
#property int score;
#end
Player.m has:
#import "Player.h"
#implementation Player
- (id)init {
self = [super init];
if (self){
_score = 5000;
}
return self;
}
#end
main.m has
#import <Foundation/Foundation.h>
#import "Player.h"
int main(int argc, const char * argv[])
{
#autoreleasepool {
Player *firstPlayer = [[Player alloc] init];
NSLog(#"The default score is %i", [firstPlayer score]);
}
return 0;
}
Player.h is missing the following line which should go between the #import and #property lines:
#interface Player : NSObject
Where NSObject may be a different class, but needs to be whatever you intend to be subclassing.

Setting up a class identifier to be called when logging array data

Hey stackOverflow community. I am working through Big Nerd Ranch's Objective C book and have come across the fun chapter on defining and setting up classes (chapter 17 if you're familiar). In it the challenge has us write a program where we define a stock class with several properties and instance variables. I have been able to get the program to work as asked but I want to tinker a little with it to get it to also NSLog a stockName so I can see what stock is associated with its properties.
Basically, is there a way to make the code more concise for this block:
NSString *appleName = #"AppleInc";
[Apple setStockIdentifier:appleName];
Maybe more like this:
[Apple setStockIdentifier:"AppleInc"];
I tried setting the property as a char in the class file but couldnt get it to work. Im new to this but I'm thinking that declaring a new NSString for the stockIdentifier value is extra code that isn't needed. Any feedback would be greatly appreciated.
Below is what I have for the main file:
#import <Foundation/Foundation.h>
#import "StockHolding.h"
int main(int argc, const char * argv[])
{
#autoreleasepool {
StockHolding *Apple = [[StockHolding alloc] init];
NSString *appleName = #"AppleInc";
[Apple setStockIdentifier:appleName];
[Apple setPurchaseSharePrice:2.30];
[Apple setCurrentSharePrice:4.50];
[Apple setNumberOfShares:40];
StockHolding *HomeDepot = [[StockHolding alloc] init];
NSString *homeDepotName = #"Home Depot Inc";
[HomeDepot setStockIdentifier:homeDepotName];
[HomeDepot setPurchaseSharePrice:12.19];
[HomeDepot setCurrentSharePrice:10.56];
[HomeDepot setNumberOfShares:90];
StockHolding *Cisco = [[StockHolding alloc] init];
NSString *ciscoName = #"Cisco Inc";
[Cisco setStockIdentifier:ciscoName];
[Cisco setPurchaseSharePrice:45.10];
[Cisco setCurrentSharePrice:49.51];
[Cisco setNumberOfShares:210];
NSMutableArray *listOfStocks = [NSMutableArray arrayWithObjects:Apple, HomeDepot, Cisco, nil];
for (StockHolding *currentStock in listOfStocks) {
NSLog(#"%#, Purchase Share Price: %.2f; Current value: %.2f; Number of shares: %i",[currentStock stockIdentifier],[currentStock purchaseSharePrice], [currentStock currentSharePrice], [currentStock numberOfShares]);
}
}
return 0;
}
Below is the contents of StockHolding.h:
#import <Foundation/Foundation.h>
#interface StockHolding : NSObject
{
//char stockIdentifier;
float purchaseSharePrice;
float currentSharePrice;
int numberOfShares;
}
#property NSString *stockIdentifier;
#property float purchaseSharePrice;
#property float currentSharePrice;
#property int numberOfShares;
-(float) costInDollars; //purchaseSharePrice * numberOfShares;
-(float) valueInDollars; //currentSharePrice * numberOfShares;
#end
And here is StockHolding.m:
#import "StockHolding.h"
#implementation StockHolding
#synthesize currentSharePrice, purchaseSharePrice, numberOfShares, stockIdentifier;
-(float)costInDollars;
{
return (purchaseSharePrice * numberOfShares);
}
-(float)valueInDollars;
{
return (currentSharePrice * numberOfShares);
}
#end
This is simply a syntax error.
[Apple setStockIdentifier:"AppleInc"];
Should be...
[Apple setStockIdentifier:#"AppleInc"];

any mistakes using #property and #synthesize

I'm a newbie of Objective-C language and I'm trying to understand the basic concepts. I came from Java language so I have already know the principle of OOP programming. Here it is the code I wrote. It's very simple but it doesn't work properly. I have some issues using #property and #synthesize
#import <Foundation/Foundation.h>
#interface Car: NSObject
#property(nonatomic,retain) NSString *brand;
#property int year;
#end //Car Interface
#import "Car.h"
#implementation Car
#synthesize brand;
#synthesize year;
#end //Car Implementation
#import "Car.h"
int main (int argc, const char * argv[])
{
int y;
//Creo un nuovo oggetto
Car *myCar = [[Car alloc] init];
//Setto i parametri
[myCar setbrand: #"BMW Z4"];
NSLog (#"Inserisci data modello: ");
scanf (" %i", &y); //E' buona norma lasciare uno spazio
[myCar setyear: y];
//Stampo a video i dati
NSLog(#"Marca: %# Anno: %i", [myCar setbrand], [myCar setyear]);
}
I don't know where it's the error. I'm pretty sure there is some mistakes in main function. is it correct call that methods in that way?
The setters must be capitalized properly.
[myCar setBrand:#"BMW Z4"];
[myCar setYear:2010];
The getters default to the property name.
NSString *carBrand = [myCar brand];
int carYear = [myCar year];

What the output should be there?

The output should be
strString = değiştim
wkString = NULL
but it is not. WHY?
#import <Foundation/Foundation.h>
#interface learnARC : NSObject {
NSString *strString, __weak *wkString;
}
#property (strong) NSString *strString;
#property (weak) NSString *wkString;
-(void) yaz;
#end
#import "learnARC.h"
#implementation learnARC
#synthesize wkString, strString;
-(void) yaz {
NSString *anaString = #"anaString";
strString = anaString;
wkString = anaString;
NSLog(#"\nstrString = %#\nwkString = %#",strString,wkString);
anaString = #"değiştim";
NSLog(#"\nstrString = %#\nwkString = %#",strString,wkString);
}
#end
int main(int argc, const char * argv[]) {
#autoreleasepool {
learnARC *lrnarc = [[learnARC alloc]init];
[lrnarc yaz];
}
return 0;
}
WHY?
Because you're captalizing your question instead of your class names...
Seriously, the weak reference should not be NULL. You have assigned a pointer to it (a pointer to the string #"anaString"). And since string literals have static storage duration, they are never deallocated during the lifetime of the program. (I think you may be confusing variables with properties?)