Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

superkappa

macrumors newbie
Original poster
Mar 28, 2012
18
0
I'm trying to create an array (only if doesn't exist) that have to be composed by both int, strings and other arrays.
This is what i'm doing but it doesen't works.
Thank you

Code:
if (! [mixedArray count]){
    NSArray *mixedArray =  [[NSArray alloc] init];
    mixedArray[0] = 5;
    mixedArray[1] = @"string";
    mixedArray[2] = otherArray;
}
 
Why would you need to do that? It sounds like if there's no pattern to the items it'd be a kinda cryptic mess to go through.

I think what you want is an array of objects that contain an int, string, and perhaps something else. However, if you're going to do this use NSStrings. If 'cookie' was an NSString, you could get its int value by asking for cookie.intvalue.
 
Sounds like you want an array of objects (i.e., type NSObject), and then deal with figuring out what the actual objects in the array are at run-time via introspection.


But, to get into this situation, one has to ask, wtf does your data model look like?

Even if you COULD easily create an array like that, it's kinda digging a hole for yourself in terms of code maintainability, etc.
 
i want to store some informations about some machines.
No one who knows how to help me?

If, for simplicity, I wanted to create an array of objects, how should I be done to understand if it exists and create it only if it exists?
 
i want to store some informations about some machines.
No one who knows how to help me?


You've been given advice - you just haven't considered it or looked into it.

If you want to store information about machines, you'd probably be better off to build an object to contain that information (eg, machine - which includes properties such as ip address, hostname, or whatever), and then create an array of those objects.


edit:
highly suggest you look up some objective-c documentation... you can't just init an array and attempt to add objects to it like that. for a start, arrays aren't mutable, you'd be wanting an NSMutableArray for that.

Do you have an account at developer.apple.com? they do free accounts where you can access the developer documentation...
 
Last edited:
If you want to store information about machines, build an object to contain that information (eg, machine - which includes properties such as ip address, hostname, or whatever), and then create an array of those objects.

Can you help me with some code?
 
I think I'd genuinely like to help you with that. If you're using xcode here's how I'd go:

Insert a new obj-c class (can be however you wish). Just inherit from NSObject.

Inside the interface

Code:
@interface blah : NSObject
{
    int number;
    NSString * name;
}
@end
Then in implementation, whatever is necessary. you might need synthesize and properties for both the implementation/interface (too sleepy here)

In your main code:
Code:
NSMutableArray * blahArray;
blahArray = [[NSMutableArray alloc] init];
blah * newBlah;
newBlah = [[blah alloc] init];

[blahArray addObject:newBlah];
And so on.

As for the set/get from array and comparing, there are tons and tons of options for how to do that. In short: lots of googling. XD sorry for not being more helpful
 
Last edited by a moderator:
Can you help me with some code?


See Joe's code above for the general idea. Its been a while since I've written any objc and have just moved house so all my reference material is in boxes; anything I give you will likely be not 100%.


And besides, if we spoon feed you code you won't learn how to do it yourself (cut/paste will only get you so far) :p

But definitely sign up at developer.apple.com if you haven't already - the free account still gets you developer docs.
 
Code:
if (! [mixedArray count]){
    NSArray *mixedArray =  [[NSArray alloc] init];
    mixedArray[0] = 5;
    mixedArray[1] = @"string";
    mixedArray[2] = otherArray;
}

You have several serious mistakes here:

1. NSArray is immutable. You can't modify immutable objects.

2. Items in an NSArray are not referenced by [subscript], but by an Objective-C message selector. You should look it up in the reference doc for the NSArray class. To find the online class reference, google search terms: NSArray class reference

3. An NSArray can only hold objects. It can't hold plain numbers like 5. See the NSNumber class.


Frankly, the posted code looks like you don't have a good understanding of the fundamentals. You should study those before trying to go farther. If you're not studying from a book or online tutorial, you should be. If you are, what is the exact title and author of the book, or the complete URL of the tutorial?
 
It don't works.

Code:
if (! [machineArray count] ){
    NSMutableArray *machineArray;
    machineArray = [[NSMutableArray alloc] init];
}

WHYYYYYYYYYYYYYYYYY?
I'm going mad :eek:

Xcode says Use of undeclared identifier 'machineArray'
 
It don't works.

Code:
if (! [[COLOR="Red"]machineArray[/COLOR] count] ){
    NSMutableArray *[COLOR="Blue"]machineArray[/COLOR];
    machineArray = [[NSMutableArray alloc] init];
}

WHYYYYYYYYYYYYYYYYY?
I'm going mad :eek:

Xcode says Use of undeclared identifier 'machineArray'
It should be pointing you at the one I've hilited in red. Is it?

You then make a mistake, by declaring a local variable named machineArray (blue). This is a completely different variable than the one referred to by the red name. If you didn't realize this, then you need to go back and study how variables are declared and why (or why not).

There are other mistakes, such as alloc'ing and then leaking, but we need to know exactly what you want to accomplish in order to address those.


So please explain exactly what you want to accomplish.

Do you already have an instance variable named machineArray? If so, show the code for it. The complete code, in the complete @interface of the class. Not just isolated fragments.

If you don't already have an instance variable named machineArray, is that what you want? If you don't know, or you don't know what an instance variable is, then you need to explain overall what you're trying to do.

Again, this looks like you don't have a good understanding of the fundamentals. You can't progress without that understanding, yet you seem unwilling to help us help you by telling us what you're using to learn.
 
What is the state of machineArray in the conditional (highlighted in red)?

machineArray is a class made in this way.
Code:
@interface machineArray : NSObject
{
    int number;
    NSString * name;
}
@end
What means "What is the state"?
 
machineArray is a class made in this way.
Code:
@interface machineArray : NSObject
{
    int number;
    NSString * name;
}
@end
machineArray is a class. It's not a variable.

Go back to post #8 and carefully reread all of JoeG4's code. In particular, pay attention to the second piece of code. If you don't understand what it's doing, or how to take it to the next step, then you need to study the fundamentals.
 
I'm making some tests and the code that I had writed in main is only this:

Code:
-(IBAction)addMachine:(id)sender{
  if (! [machineArray count] ){
      NSMutableArray *machineArray;
      machineArray = [[NSMutableArray alloc] init];
  }
}





I want to add an object machine in an array that it's called machineArray.
machineArray had to be created only if don't exist.




the code of the class is only this:
Code:
@interface machineArray : NSObject
{
    int number;
    NSString * name;
}
@end
 
machineArray is a class made in this way.
Code:
@interface machineArray : NSObject
{
    int number;
    NSString * name;
}
@end
It seems you have based your code on what JoeG4 supplied in this post. But, as chown33 already suggested, you don't have an understanding of the fundamentals in order to use it properly. You should step away from the real coding and take some time to learn the fundamentals of Objective-C programming. If you are already using resources to do so, you should be courteous to chown33's request and state what those are.

What means "What is the state"?
By this I'm asking, "At this point, has machineArray been declared? Has it been assigned a value?" Things like that.

----------

machineArray had to be created only if don't exist.

Checking the count property of an array is not a good way to see if a variable "exists".
 
I'm a newbie for object oriented languages.
I studied some guides, but I would like me to do practically.
If someone want to help me I would be grateful! :)
 
I'm a newbie for object oriented languages.
Nothing wrong with that.

I studied some guides, but I would like me to do practically.
What guides? You been asked to provide details of the resources you're using and yet you continue to ignore this request. Why?

If someone want to help me I would be grateful! :)
First, you must be respectful of those you seek help from and answer their questions. You must help us first before we can help you.
 
What guides? You been asked to provide details of the resources you're using and yet you continue to ignore this request. Why?

I did not answer because I did not think was so important to know which guide I studied and especially since not enter into the details of programming language.
The one I use as reference is
http://www.otierney.net/objective-c.html
and i have a book writed in italian.

Now someone can say me how to create an array only if doesen't exist?
Thank you!
 
Now someone can say me how to create an array only if doesen't exist?

I could. But without an understanding of the fundamentals of Objective-C, my answer would probably be meaningless to you. Kind of like if you had posted your question in Italian; most of us wouldn't understand since we don't know the fundamentals of the Italian language. Capisce? ;)
 
Try. I understand fastly!

P.S. Can I study all the English on the books, but if I do not speak do not ever learn!
 
You need to go get a book on Objective C or object oriented programming in general. People on the forum don't mind helping but we are here for free and expect you to put in some effort on your own.

Your issue is that you are trying to use the pointer "machineArray" before you create and initialize it.

Also you won't want to create your array inside the method you will be calling to add items to that array. Look up Variable Scope.

Do yourself a favor and work through a basic Objective C book and do all of the exercises.
 
Do yourself a favor and work through a basic Objective C book and do all of the exercises.

Yes. Do this.

You, the OP, don't understand the difference between the class name machineArray and a variable named machineArray. This is fundamental, and trying to get it only by reading code won't work. You need the explanation of what a class is, what an instance of that class is, and how instances vs. classes work. None of that is going to come from the otierney tutorial.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.