Create an array with both int, strings and other arrays

Discussion in 'iOS Programming' started by superkappa, Mar 28, 2012.

  1. superkappa macrumors newbie

    superkappa

    Joined:
    Mar 28, 2012
    #1
    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;
    }
     
  2. JoeG4 macrumors 68030

    JoeG4

    Joined:
    Jan 11, 2002
    Location:
    Bay Area, Ca.
    #2
    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.
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
  4. throAU macrumors 601

    throAU

    Joined:
    Feb 13, 2012
    Location:
    Perth, Western Australia
    #4
    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.
     
  5. superkappa thread starter macrumors newbie

    superkappa

    Joined:
    Mar 28, 2012
    #5
    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?
     
  6. throAU, Mar 28, 2012
    Last edited: Mar 28, 2012

    throAU macrumors 601

    throAU

    Joined:
    Feb 13, 2012
    Location:
    Perth, Western Australia
    #6

    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...
     
  7. superkappa thread starter macrumors newbie

    superkappa

    Joined:
    Mar 28, 2012
    #7
    Can you help me with some code?
     
  8. JoeG4, Mar 28, 2012
    Last edited by a moderator: Mar 28, 2012

    JoeG4 macrumors 68030

    JoeG4

    Joined:
    Jan 11, 2002
    Location:
    Bay Area, Ca.
    #8
    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
     
  9. throAU macrumors 601

    throAU

    Joined:
    Feb 13, 2012
    Location:
    Perth, Western Australia
    #9

    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.
     
  10. superkappa thread starter macrumors newbie

    superkappa

    Joined:
    Mar 28, 2012
    #10
    Thank you.
    I'll try. If I have problems I'll post them here.
     
  11. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #11
    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?
     
  12. superkappa thread starter macrumors newbie

    superkappa

    Joined:
    Mar 28, 2012
    #12
    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'
     
  13. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    What is the state of machineArray in the conditional (highlighted in red)?
     
  14. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #14
    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.
     
  15. superkappa thread starter macrumors newbie

    superkappa

    Joined:
    Mar 28, 2012
    #15
    machineArray is a class made in this way.
    Code:
    @interface machineArray : NSObject
    {
        int number;
        NSString * name;
    }
    @end
    
    What means "What is the state"?
     
  16. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #16
    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.
     
  17. superkappa thread starter macrumors newbie

    superkappa

    Joined:
    Mar 28, 2012
    #17
    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
    
     
  18. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #18
    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.

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

    ----------

    Checking the count property of an array is not a good way to see if a variable "exists".
     
  19. superkappa thread starter macrumors newbie

    superkappa

    Joined:
    Mar 28, 2012
    #19
    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! :)
     
  20. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #20
    Nothing wrong with that.

    What guides? You been asked to provide details of the resources you're using and yet you continue to ignore this request. Why?

    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.
     
  21. superkappa thread starter macrumors newbie

    superkappa

    Joined:
    Mar 28, 2012
    #21
    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!
     
  22. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #22
    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? ;)
     
  23. superkappa thread starter macrumors newbie

    superkappa

    Joined:
    Mar 28, 2012
    #23
    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!
     
  24. jonnymo5 macrumors 6502

    Joined:
    Jan 21, 2008
    Location:
    Texas
    #24
    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.
     
  25. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #25
    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.
     

Share This Page