dictionaries, arrays and pointers... Serious objective-c question

Discussion in 'iOS Programming' started by grandM, Aug 7, 2014.

  1. grandM, Aug 7, 2014
    Last edited by a moderator: Aug 26, 2014

    grandM macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #1
    Hi everyone

    During my studies about iOS (Stanford course) I came across following code and it got me thinking about a lot of things. Here's the code in FlickrHelper's implementation:

    Code:
    + (NSDictionary *)placesByCountry:(NSArray *)places
    {
        NSMutableDictionary *placesByCountry = [NSMutableDictionary dictionary];
        for (NSDictionary *place in places) {
            NSString *country = [FlickrHelper countryOfPlace:place];
            NSMutableArray *placesOfCountry = placesByCountry[country];
            if (!placesOfCountry) {
                placesOfCountry = [NSMutableArray array];
                placesByCountry[country] = placesOfCountry;
            }
            [placesOfCountry addObject:place];
        }
        return placesByCountry;
    }
    
    + (NSString *)countryOfPlace:(NSDictionary *)place
    {
        return [[[place valueForKeyPath:FLICKR_PLACE_NAME]
                 componentsSeparatedByString:@", "] lastObject];
    }
    
    Btw: places looks like this (with many more elements in the dictionary).
    Code:
    places: (
            {
            "_content" = "Bom Jesus da Lapa, Bahia, Brazil";
            latitude = "-13.249";
            longitude = "-43.409";
            "photo_count" = 274;
            "place_id" = YWVVOUBVV7ya3PE;
            "place_type" = locality;
            "place_type_id" = 7;
            "place_url" = "/Brazil/Bahia/Bom+Jesus+da+Lapa";
            timezone = "America/Bahia";
            "woe_name" = "Bom Jesus da Lapa";
            woeid = 456084;
        },
            {
            "_content" = "\U0141\U00f3d\U017a, Lodz, Poland";
            latitude = "51.761";
            longitude = "19.468";
            "photo_count" = 90;
            "place_id" = Yg5Az8ZUUr8ITa4;
            "place_type" = locality;
            "place_type_id" = 7;
            "place_url" = "/Poland/Lodz/%C5%81%C3%B3d%C5%BA";
            timezone = "Europe/Warsaw";
            "woe_name" = "\U0141\U00f3d\U017a";
            woeid = 505120;
        }}
    Here are the questions:
    1 Why is a dictionary being returned? Couldn't it be an NSArray. Because an NSArray could also be a collection of dictionaries. Or wouldn't it be possible to do placesByCountry[country] as country is a string?

    2
    Code:
    NSMutableArray *placesOfCountry = placesByCountry[country];
    If this element doesn't exist will it return nil or crash?

    3 PlacesOfCountry is contained within the for-loop. So PlacesOfCountry doesn't set to nil as long we're in the for-loop (or does it go to nil with each iteration of the for-loop?). Am I correct assuming that by repeating NSMutable Array *placesOfCountry the following happens:
    A say placesByCountry[country] contains 4, the pointer refers to memory place starting at 4
    B say placesByCountry[country] contains 896, the pointer refers to memory place starting at 896
    C say placesByCountry[country] contains nothing, the pointer is set at nil and is giving a pointer value by the alloc init in the if-clause.
    In this case the same variable name placesOfCountry would possibly be shifting to different memory places in each iteration of the for-loop.

    4 Why would you go saying then
    Code:
    NSMutableArray *placesOfCountry = placesByCountry[country];
    . Wouldn't it be better to shift the declaration before the for-loop starts?

    tx !
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    What this code does is sort all the places into lists where all the items in each list are from the same country. It stores each list of places in a dictionary where the key is the country name.

    1. Without the dictionary you don't know what country each list represents.

    2. return nil.

    3. The code doesn't know how many countries there are before the loop starts. There needs to be one instance of placesOfCountry for each country. The way the code is written it creates a new placesOfCountry list whenever it finds a new country.
     
  3. Dookieman macrumors 6502

    Joined:
    Oct 12, 2009
    #3
    Correct. I had this exact issue a few months ago when I was trying to figure out a way to display all items for a key only once, even if they occurred multiple times.

    The code is a bit confusing on how to accomplish it...
     
  4. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #4
    ok tx guys
    This iOS and objective-c is taking much more time than I ever anticipated. I hoped to realize some commercial projects but I guess it will more become a hobby. By the time I 've mastered it pretty much everything will already exist and objective-c will be swifted. Really never thought it was so hard. In my opinion a good programmer ought to earn MORE than whatever CEO.
     
  5. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #5
    Swift won't replace Objective-C any time soon. Don't worry about it, keep going with Objective-C.

    As for everything will already exist, there will always be opportunities to create new software. You just have to think outside the box a bit. Don't lose hope though. Keep going and you'll reap the rewards later on. I come up with new ideas regularly. I'm more constrained with time than I am when it comes to ideas (I can really only work on one thing at a time).

    Programming is hard. Learning the programming language itself is the easy bit but learning how to make effective use of the language to solve real world problems is where the expertise is needed.
     
  6. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #6
    How long ago did you start? How much time have you been putting in per day?

    You don't need to know much to make money. More, you need to think of something that doesn't already exist that people will pay money for. It's not necessarily complex or difficult to create.

    I kind of doubt that everything will already exist - there will always be new things to create. Until someone makes a program that can produce other programs with only a problem statement. Which I don't anticipate being here before 2030. So you have time.

    I disagree. You should start off as a good programmer, then you should become a team leader that can recruit, mentor, and lead other programmers. This causes you to become several times more useful than you used to be. Then you should become a leader of team leaders. Until ultimately you're at the head of the software division of your company. Then if you also are talented in the other things your company does (it probably has a marketing department in addition to the software division if nothing else,) then you could be a CEO. Who could get down and dirty with the code, but instead leads the person who leads the people who leads the programmers. IE, Bill Gates or Mark Zuckerberg are programmers who became CEOs.
     
  7. Dookieman macrumors 6502

    Joined:
    Oct 12, 2009
    #7
    When did you start programming? If you're doing it in your spare while you're at your day job or in school studying something else, it will take time.

    You're right, programming is hard, and it sounds like you like the idea of knowing how to program, but don't know exactly what you want to write, so you're going to lose motivation.

    What makes programming even harder is getting all motivated to learn, taking a few days or weeks looking over tutorials and books, then losing motivation because it CAN be very boring, dull, and confusing when you're going over Arrays, Strings, and Dictionaries etc... It happened to myself and probably most people here at some point. I can't imagine someone can picking up a book on C and reading it cover to cover without writing an application on the side to reenforce what was read.

    My advice to you is find a project that gets you REALLY excited to program. Like anxiously looking forward to get home and get working on. Dive right in, you'll learn things along the way, make mistakes, get frustrated but the bumpy road is worth it.
     
  8. grandM, Aug 18, 2014
    Last edited: Aug 18, 2014

    grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #8
    Hi tx for the advice. Actually I am thinking of some things to program. But I had decided to first learn to program properly. At first I was fumbling around and if I had started the implementation of the idea then the code would have been crap.

    It's just a shame it takes a while. I indeed have lost motivation at some point to. So there have been gaps between my studying. But it is very challenging and hard to be honest. I never could program fulltime. Being unemployed I tried to find a job opening each day. Then wrote a letter, then did some objective-C. Afterwards objective-c was replaced by iOs.

    The frustrating part is just that you want to get going. But I know I must complete the course first or I'll be doing things the wrong way. It's that simple. It's frustrating nevertheless. It didn't help they are about to replace objective-c by swift. Then I REALLY cursed out. Guess I wish I will end up having launched at least one idea programmed by myself one day (I am not even talking of being successful). Really would hate the learning would end up in well learning.

    ----------

    there have been gaps between my studying. But it is very challenging and hard to be honest. I never could program fulltime. Being unemployed I tried to find a job opening each day. Then wrote a letter, then did some objective-C. Afterwards objective-c was replaced by iOs.

    The things we were thinking about aren't easy to be honest.

    Lol at my grave: he was just about to finish the Stanford course. Now HE is finished.

    Perhaps that 's the case in the States. On the Old Continent things are still less competence oriented and more relationship oriented. I've seen a lot of people who weren't that competent become manager. And I've seen a lot of people who were really competent not getting a chance. HR bust people on minor glitches never letting them in. Over some time your curriculum is full of temps and well you're out. Believe me if I say: I know CEO's of major institutions which greatest benefit is their network.

    This network they got by just being born in the right circles. I admit a guy like Jobs I admire. Not for the kind of man he was but because he made it HIMSELF. Gates is a bad example: he could barely program. Gates is from a very distinctive and rich background. I have very little respect for this kind of people. I wonder how successful they would have been if their parents were blue collar workers (like mine). Lol I remember this 3-day seminar I was on. I've beaten a bunch of those CEO's and they admitted it themselves. You had to see them looking. But at the end of the day they had earned a few thousand dollars and I a hundred. Believe me when I say I have studied with a lot of those managers. I'm still puzzled how they became manager. Guess that explains why so many firms take bad decisions. For Jobs however I take my hat off. He did it himself and I wouldn't have been able to do so!

    By the way: I am in my late thirties and recruiters are still asking me what the profession of my parents is... I'd like to skin them alive when they ask this. Next time I'll answer by asking what their cup size is. Perhaps they will get it then...
     
  9. Dookieman macrumors 6502

    Joined:
    Oct 12, 2009
    #9
    You're approaching it the wrong way. You won't learn how to program the way Apple, Google, EA, Capcom, Adobe, whatever big name company or studio does right away. That takes time and lots of trial and error.

    What does knowing how to code properly even mean? There a ton a different ways to implement the same idea, as long as they work, it's still proper code right? Now, there are best practices and guidelines you should follow, but those will be learned over time and experience. There is nothing wrong with going back and fixing something that works with a better API or approach.

    BTW, there are TONS of terrible programs on the app store. I'm not suggesting that you start writing crappy apps, but there are about 1,000 fart apps, or used to be anyways, and I bet a lot of those are beginners just trying to get their feet wet and an app to their name.

    I'm not suggesting I'm a pro and most people here are more experienced than I am, but this is what worked for me.
     
  10. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #10
    Well I understand your approach completely. It's certainly less frustrating than studying to get it right. I must admit though after my first book of iOs I am now learning a lot of the Stanford course. It's the way it should be done (as for good practices is concerned). So I guess my approach has the advantage my initial code will be more like pro's like it. Lol, if I get it working anyways. But I certainly get your drift.
     

Share This Page