views:

125

answers:

6

I have understandably attracted some smart ass answers to some bone head questions I have asked lately due to my complete misunderstanding of obj c and NSMutable arrays. So I've looked in a book I have and Google and it would appear that may be I shouldn't program because I'm still stuck.

Say I'm looping through method A and each loop through I want to add a double into a NSMutable array. This is what I've discovered so far.

You can't put primitives in arrays, so I made my double into a NSNumber object

NSNumber *newObject = [NSNumber initWithDouble:doubleNumber];

XCode warns: 'NSNumber' may not respond to '+initWithDouble:'

and when I compile it get:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSNumber initWithDouble:]: unrecognized selector sent to class 0x1ffea0'

I ignore that and forge on. Some one told me I need an init method for the array, which makes sense because I don't want that happening in the loop. This is my init method:

-(id) init{
    [super init];
    arrayOfTouches = [[NSMutableArray alloc] init];
    return self;
}

I haven't got to try to add it to the array yet.

[arrayOfTouches addObject:newObject];

Will this work to print out the array.

NSLog(@"array: %@", touchBeginObj);

Bonehead questions, probably. But damn if I can find anything that has all the pieces about what you can and can't do with an good example.

Thanks for any help or smart ass comments.

Yes, I have a Obj-C book, 3 actually and yes I'm reading them :)

Have a good weekend.

+3  A: 

Change this:

NSNumber *newObject = [NSNumber initWithDouble:doubleNumber];

to this:

NSNumber *newObject = [NSNumber numberWithDouble:doubleNumber];

-initWithDouble is an instance method of NSNumber (so you would need to do [NSNumber alloc] first). +numberWithDouble is a class method.

aBitObvious
+10  A: 
NSNumber *newObject = [NSNumber initWithDouble:doubleNumber];

You’re missing the +alloc call in the above code, which is why it’s throwing that error—but why not just use:

NSNumber *newObject = [NSNumber numberWithDouble:doubleNumber];

It’s autoreleased, so you don’t have to worry about releasing it later.

I’m not sure what either of the other questions are asking, could you possibly rephrase them? From the code you’ve given, I think it should work

James Raybould
+2  A: 

initWithDouble is an instance method, you're looking for the class method numberWithDouble.

John Franklin
Mr. John Franklin, the relief I felt when my number printed in the console. numberWithDouble. ahhhh some relief Thanks!
rd42
Some how the NSLog(@"array: %@", arrayOfTouches); still prints out array: (null)
rd42
Are you setting `arrayOfTouches` to nil somewhere? Are you certain you're initializing the `arrayOfTouches`? `nil` can be passed any message, and will quietly ignore it. So `NSMutableArray *foo = nil; [foo addObject:bar];` will compile and run, but obviously `bar` is never really saved. Put in a couple breakpoints, one in the `init` method (make sure it's called) an one at the `NSLog` line to see if it's called before `init`.
John Franklin
+5  A: 

First :

[NSNumber numberWithDouble: double]

instead of

[NSNumber initWithDouble: double]

Your addObject looks fine.

You have several ways to print your array, I'd suggest iterating, as it will be useful later:

for (NSNumber *number in arrayOfTouches)
{
    NSLog(@"Number %f", [number doubleValue]);
}
jv42
Thanks for the print statement, it worked great!
rd42
+1  A: 

Go to Help-->Developer Documentation

Search for NSNumber. Under class methods you'll find how to create a NSNumber with a double.

Michael Kernahan
+1  A: 

You need to have zero warnings from the compiler - you can't ignore them and 'forge on'. You are struggling with the basics, do you have experience of other programming languages?

There are a few things you must know in order to code in Objective-c and you must be familiar with the general concepts of object-oriented programming. Also a little C can be helpful as objective-c is an extension to C and it can give some perspective as to why object orientation is so useful and worth-the-effort.

If you already know a language like Python, Java, or even Actionscript then pretty much all of the concepts are the same and objective-c really isn't that difficult. You just need the specifics on how those concepts translate into objective-c. For example,

How do you define a class? How do you override an existing class? What does an initializer method look like? How do you create an instance of a class? etc. There are a few more things than this, but really not that many - you need to learn them, or at least have references at hand in the beginning so you don't go wrong. If you don't know what they mean then you need to learn that.

A 'Class' is like a blueprint for the layout of a piece of memory, in the case of NSNumber a piece of memory that we would hope is going to hold a number. The NSNumber Class knows how big the memory has to be and how it should be laid out. We really care little about the blueprint, the Class - we want an actual chunk of memory that can hold a number - an instance of the Class.

In objective-c creating an object, an instance, of a given class always, everytime, always, always involves -- firstly -- claiming ownership of a free chunk of memory with the correct size, saving the address of that chunk of memory to a variable, then giving the object a chance to do it's own initial setup by calling it's preferred setup method.

To claim ownership of the memory you send the alloc method to the class.

NSNumber *myNumberVariable = [NSNumber alloc];

To set the number up with the initial value of 10.0 you send the message initWithDouble: to your new instance.

[myNumberVariable initWithDouble:10.0];

For convenience these can be, and usually are combined into one statement.

NSNumber *myNumberVariable = [[NSNumber alloc] initWithDouble:10.0];

This is the most fundamental aspect of objective-c programming. You cannot do anything without thoroughly understanding this. Googling init objective-c returns literally hundreds of tutorials, guides, cheat-sheets and help.

If your own Class needs a property initializing when an instance is created, like your arrayOfTouches, you must override the preferred setup method of the parent Class. If the parent Class is NSObject, NSObject's preferred setup method is init. If you create an object in your setup method you must always have a dealloc method. This will always, always, all of the time look like this:

- (id)init {
  self = [super init];
  if(self){

  }
  return self;
}
- (void)dealloc {
  [super dealloc];
}

Only the name "init" will change, depending on the name of the method you are overriding, - (id)init becomes - (id)nameOfMethodIAmOverriding and [super init] becomes [super nameOfMethodIAmOverriding];

Given this standard template for setup and teardown you can add in the setup and teardown of your own properties. In your case giving you.

- (id)init {
  self = [super init];
  if(self){
    arrayOfTouches = [[NSMutableArray alloc] init];
  }
  return self;
}
- (void)dealloc {
  [arrayOfTouches release];
  [super dealloc];
}

Look, it's not that i think you have Boneheaded questions, they are sensible questions but if you have 3 books - the answers are almost certainly on the first few pages of each book - maybe you are going about learning this wrong. Somehow you are not getting the fundamentals. The best i can suggest is to go more slowly and do the exercises in the book, Or choose a simpler language and attempt to master that before moving to objective-c.

mustISignUp
Thanks!! I only have experience in PHP and only using it in a procedural way. I really appreciate the time you took on this response. Thanks again!
rd42