I have a program which runs fine, but I get no print output even though I have a NSLog file in main.m Can you tell me what's wrong? Thank you.
main.m
#import <Foundation/Foundation.h>
#import "Stockholding.h"
int main(int argc, const char * argv[])
{
#autoreleasepool {
StockHolding *stockA;
StockHolding *stockB;
StockHolding *stockC;
[stockA setPurchaseSharePrice:2.40];
[stockA setCurrentSharePrice:3.12];
[stockA setNumberOfShares:40];
[stockB setPurchaseSharePrice:1.50];
[stockB setCurrentSharePrice:1.41];
[stockC setNumberOfShares:35];
[stockC setPurchaseSharePrice:1.10];
[stockC setCurrentSharePrice:1.20];
[stockC setNumberOfShares:60];
NSArray *holdings = [NSArray arrayWithObjects:stockA, stockB, stockC, nil];
for (StockHolding *n in holdings) {
// Call the methods
float cost = [n costInDollars];
float value = [n valueInDollars];
NSLog(#"Bought stock for $%.2f, It is now at $%.2f, I have %d shares, They cost me $%.2f, Now they are worth $%.2f", [n purchaseSharePrice], [n currentSharePrice], [n numberOfShares], cost, value);
}
}
return 0;
}
StockHolding.h
#import <Foundation/Foundation.h>
#interface StockHolding : NSObject {
float purchaseSharePrice;
float currentSharePrice;
int numberOfShares;
}
#property float purchaseSharePrice;
#property float currentSharePrice;
#property int numberOfShares;
-(float)costInDollars;
-(float)valueInDollars;
#end
StockHolding.m
#import "StockHolding.h"
#implementation StockHolding
#synthesize purchaseSharePrice, currentSharePrice, numberOfShares;
-(float)costInDollars
{
return (purchaseSharePrice * numberOfShares);
}
-(float)valueInDollars
{
return (currentSharePrice * numberOfShares);
}
#end
You haven't actually created any of those StockHolding objects. Thus, your array is empty, and the loop doesn't do anything.
StockHolding *stockA;
is just a declaration of a pointer. You need to create the object to which it points; the usual procedure is this:
StockHolding *stockA = [[StockHolding alloc] init];
Since, under ARC, object pointers are initialized to nil (which means "no object"), you are passing nil as all of the arguments to arrayWithObjects:. nil being the sentinel value meaning "there are no more arguments", the array is created without contents.
With an empty array, for (StockHolding *n in holdings) doesn't have anything to enumerate over, so none of the code in the body of the loop, including your NSLog(), gets executed.
Related
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];
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"];
I wrote two programs during a training. But one exercise task drives me crazy. Not the task itself but the program and the behavior of it.
The first program is to calculate the BMI of a person. Here it works all fine.
main.m
#import <Foundation/Foundation.h>
#import "Person.h"
int main (int argc, const char * argv[])
{
#autoreleasepool {
// Erstellt eine Instanz von Person
Person *person = [[Person alloc]init];
// Gibt den Instanzvariablen interessante Werte
[person setWeightInKilos:93.2];
[person setHeightInMeters:1.8];
// Ruft die Methode bodyMassIndex auf
float bmi = [person bodyMassIndex];
NSLog(#"person (%dKg, %.2fm) has a BMI of %.2f", [person weightInKilos],
[person heightInMeters], bmi);
}
return 0;
}
Person.h
#interface Person : NSObject
{
// Sie hat zwei Instanzvariablen
float heightInMeters;
int weightInKilos;
}
// Sie können diese Instanzvariablen anhand folgender Methoden setzen
#property float heightInMeters;
#property int weightInKilos;
// Diese Methode berechnet den Body-Mass-Index
- (float)bodyMassIndex;
#end
Person.m
#import "Person.h"
#implementation Person
#synthesize heightInMeters, weightInKilos;
- (float)bodyMassIndex
{
float h = [self heightInMeters];
return [self weightInKilos] / (h * h);
}
#end
This program was written by the author of the training.
My task is to write a program what is equal.
In my opinion it looks exactly the same:
main.m
#import <Foundation/Foundation.h>
#import "StocksHolding.h"
int main (int argc, const char * argv[])
{
#autoreleasepool {
StocksHolding *stock1 = [[StocksHolding alloc]init];
[stock1 purchaseSharePrice:1];
/*Here I geht the error "Instance method '-purchaseSharePrice:' not found (return type
defaults to 'id')*/
NSLog(#"%i", [stock1 purchaseSharePrice]);
}
return 0;
}
StockHoldings.h
#import <Foundation/Foundation.h>
#interface StocksHolding : NSObject
{
int purchaseSharePrice;
float currentSharePrice;
int numberOfShares;
}
#property int purchaseSharePrice;
#property float currentSharePrice;
#property int numberOfShares;
- (float)costInDollars;
- (float)valueInDollars;
#end
StockHoldings.m
#import "StocksHolding.h"
#implementation StocksHolding
#synthesize purchaseSharePrice, currentSharePrice, numberOfShares;
- (float)costInDollars
{
return purchaseSharePrice * numberOfShares;
}
- (float)valueInDollars
{
return currentSharePrice * numberOfShares;
}
#end
AS you can see... almost no differences except the names of the variables and methods.
Where is the error?
I was sitting for 3 hours at this question.
Please give me a helping hand.
Thanks
Christian
The problem is that you are not using the generated setter method.
purchaseSharePrice is a property.
The default setter is setPurchaseSharePrice: and the default getter is purchaseSharePrice.
So you can just do this
[stock1 setPurchaseSharePrice:1];
or this
stock1.purchaseSharePrice = 1;
In addition, when you want to get the value using the generated getter you can do
int myPrice = [stock1 purchaseSharePrice];
or this
int myPrice = stock1.purchaseSharePrice;
As you can see for both setting and getting, having a property allows you to use dot syntax with the property name directly, while using method syntax requires you to use the method names generated by the property.
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?)
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;
}