Need help using NSString

Discussion in 'iPhone/iPad Programming' started by Beadon99, Nov 30, 2009.

  1. macrumors newbie

    Joined:
    Nov 30, 2009
    #1
    My friends and I are doing a project for one of our classes, it didn't seem to difficult but none of us know how to code effectively.

    Basically our project will get the file path from three text files within the bundle. From the files we will randomly pull one line from each file. From these we will make random sentences that will generally be crazy.

    We've researched a but we're still a bit confused. This is what we've got so far:

    Code:
    - (IBAction)displayRandom
    {
    	NSString *filePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"txt"];  
    	if (filePath) {  
    		NSString *myText = [NSString stringWithContentsOfFile:filePath];  
    		if (myText) {  
    			ftext.text= myText;  
    			
    		}  
    	}
    from here we aren't too sure about to go from here. Any tips or help would be greatly appreciated.
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Look at the NSString method componentsSeparatedByString: for creating an array out of all the lines of text in the file.

    You will need srandom() and random(). From there it's just generating the right range of random numbers as the array index and putting them back together with a mutable string.
     
  3. thread starter macrumors newbie

    Joined:
    Nov 30, 2009
    #3
    Could you possibly give us a code snippet? Like I said earlier, none of us know the language and most of the class is just hands down confused.
     
  4. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Isn't learning the language one of the tasks of this class?
     
  5. thread starter macrumors newbie

    Joined:
    Nov 30, 2009
    #5
    No, the the class is a project class. Basically, our instructor gives us something to do and we do it. It just so happened to be an iPhone app and we just can't seem to learn anything about it.
     
  6. macrumors regular

    Joined:
    Feb 27, 2008
    #6
    If you are not supposed to learn anything, just complete the 'project', then tell the instructor to give you exact code snippet you need to insert in the code to 'complete' the 'project'. Or read appropriate documentation.
     
  7. thread starter macrumors newbie

    Joined:
    Nov 30, 2009
    #7
    So after a bit of research, we've come up with a decent bit of code, but we're having some errors we can't find a solution to.

    Code:
    char *type[] = {"Warning, ","Alert, ","Caution, ","Danger "};
    	int *part1picked = arc4random() % 4;
    	NSString *part1word;
    	
    	char *dynamic[] = { "Conan O'Brien is ","Zombies are, Raptors are ", "Mutants are ", "Escaped Convicts are ", "Chef Boyardee is ", "Gordon Ramsey is ", "Rick Astley is ", "Vampires are ", "Frankenstein is ", "Werewolves are "};
    	int *part2picked = arc4random() % 10;
    	NSString *part2word;
    		
    	char *statik[] = { "attacking cars!", "eating people!", "starting a pyramid scheme!", "selling cookies!", "toilet papering houses!", "abusing cooking spray!", "rampaging across the highway!", "kidnapping your mom!", "cussing at old people!", "sniffing glue!", "tipping over port-o-potties!", "deep frying butter!", "selling fake jewelry!", "not honoring coupons!"};
    	int *part3picked = arc4random() % 14;
    	NSString *part3word;
    	
    	NSString *sentence;
    		
    	part1word = type[part1picked];
    	part2word = dynamic[part2picked];
    	part3word = statik[part3picked];
    		
    		sentence = [part1word stringByAppendingString:part2word stringByAppendingstring:part3word];
    The errors that we're are getting are where we choose the words from strings.
    Code:
     	
    part1word = type[part1picked];
    part2word = dynamic[part2picked];
    part3word = statik[part3picked];
    
    Here is the error: Array subscript is not an integer

    Any insight on this would be very helpful.
     
  8. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    You sure you want a pointer to an int?

    P.S. Pay attention to the warnings as well as the errors.
     
  9. thread starter macrumors newbie

    Joined:
    Nov 30, 2009
    #9
    Just wondering but, why shouldn't you have a pointer to an int?

    We didn't get any warnings about that though.
     
  10. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Because that's probably not how you want to be referencing it. int is a primitive and thus is normally handled as such. So, either lose the pointer or you need to adjust how you reference it, such as when using it as an index in an array, like in this line:
    Code:
    part1word = type[part1picked];
    You're not using a primitive value here, you're using a pointer to one.

    Interesting. When I compiled the code you supplied, I got 4 warnings and 3 errors. The first three warnings were: "Initialization makes pointer from integer without a cast" on each of the "int *part..." lines.
     
  11. thread starter macrumors newbie

    Joined:
    Nov 30, 2009
    #11
    So which would be the better route to take? Using the index of an array or use a primitive value?

    And from this, could you point me in what I should be looking at to accomplish this?
     
  12. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    I don't think you properly comprehend the options, which are: 1) use a primitive value, and 2) use a pointer to a primitive value. Using an index of an array is inevitable, if you hope to access an element of the array.

    Based on the code you supplied and how I believe you are using it, using a primitive value should be sufficient. So, start with:
    Code:
    int part1picked = arc4random() % 4;
    Next, you should to decide if you want to be dealing with a C-type strings (char *) or Objective-C-type strings (NSString). You're mixing them needlessly.
     
  13. thread starter macrumors newbie

    Joined:
    Nov 30, 2009
    #13
    I figured most of it out in the last 5 minutes or so some how. The only thing I'm having trouble with now is getting the strings into the label variables for the interface. I went with the C route, so I'm using char for my strings.
     
  14. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #14
    Care to elaborate? Are you trying to store sentence into a UILabel? What trouble are you having?
     
  15. thread starter macrumors newbie

    Joined:
    Nov 30, 2009
    #15
    Well, I've modified the existing code, so basically the variable type will be in a separate label, which is one problem I'm having. Another is trying to join string dynamic and statik into one so I can store it in a UILabel as a whole.
     

Share This Page