Need clarity on how to properly use NSMutableString

Discussion in 'iOS Programming' started by drharv, Mar 9, 2011.

  1. drharv macrumors newbie

    Joined:
    Jul 18, 2008
    #1
    I have programmed for years, but am new to xcode and objective c. I just wanted to double check my code and see if there were any problems with memory or suggestions for better ways to handle stuff. I have searched in books and online, but most places just set a string and don't do any editing/parsing.

    This is a function to take a windows folder path (entered by a user), and clean it up before saving it to a database. Please let me know if this is properly done or not. Thanks for any feedback!

    Code:
    - (NSString *)getCleanUNCPath {
    	NSMutableString *transPath = [NSMutableString stringWithString:[self.allSettings objectForKey:@"FTPTranslatedPath"]];
    
    	//Make sure all slashes are correct
    	[transPath replaceOccurrencesOfString:@"/" withString:@"\\" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [transPath length])];
    
    	//Strip leading and ending slashes (we will replace in case the have too many/not enough)
    	while ([transPath characterAtIndex:0] == '\\') {
    		[transPath deleteCharactersInRange:NSMakeRange(0, 1)];
    	}
    	while ([transPath characterAtIndex:([transPath length] - 1)] == '\\') {
    		[transPath deleteCharactersInRange:NSMakeRange(([transPath length] - 1), 1)];
    	}
    	
    	//Add the leading double slash
    	transPath = [NSMutableString stringWithFormat:@"\\\\%@", transPath];
    	//Add the ending slash
    	[transPath appendString:@"\\"];
    	
    	return transPath;
    }
    
     
  2. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #2
    Seems a bit heavy-handed to always strip and add backslashes, even though I have been prone to such behavior myself.

    If the user is entering more backslashes at the front (e.g. because they are a unix person and are escaping them out of habit) don't you think they might still do that in the middle of the UNC path and not just at the ends? \\\\server\\share\\folder\\file would be typical of that format and fully acceptable. http://en.wikipedia.org/wiki/Path_(computing)#Uniform_Naming_Convention

    B
     
  3. drharv thread starter macrumors newbie

    Joined:
    Jul 18, 2008
    #3
    You are correct, and I originally was going to replace \\ with \ in the rest of the string. I really just didn't do it yet because I wasn't sure what methods to use.

    I was going to say 'while (string contains "\\") replace "\\" with "\"', but I didn't know what method to use for the conditional part.

    Still not sure what logic I will use, but my main concern is the memory management and methodology I am using to edit the string. I will have to do a lot of this type of behavior in the rest of the program, and want to make sure I do things the best way.
     
  4. balamw, Mar 9, 2011
    Last edited: Mar 9, 2011

    balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #4
    Two thoughts:
    1. Use replaceOccurrencesOfString again.
    2. Tokenize the string (e.g. with NSScanner) using "\" as a separator and put it back together with the right delimiters.

    EDIT: For example:
    Code:
    int doubles=0;
    do {
    	doubles=[transPath replaceOccurrencesOfString:@"\\\\" withString:@"\\" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [transPath length])];	
    	NSLog(@"Doubles %d",doubles);
    } while (doubles != 0);
    
    B
     
  5. drharv thread starter macrumors newbie

    Joined:
    Jul 18, 2008
    #5
    Sweet! I didn't realize that replaceOccurencesOfString was returning the number of times it replaced.

    Thanks a lot!!

    As far as memory, do I need to release anything? I was reading on another site that I shouldn't release as long as I didn't use a method that had "new", "init", or "alloc" in it.
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    If there's any chance that the original string can be the empty string or just have a few slashes in it then length - 1 will probably crash.

    You can also break up a string like this with componentsSeparatedByString: and then put it back together however you like (without the extra slashes or with single slashes in the places you want).
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    Here are the official rules. The basic rule is short enough to memorize:
    http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html


    For future reference, you probably know that accuracy is often critical in programming. "On another site" is vague. Without a URL, it's too vague to usefully comment on. The rules of memory management are often paraphrased, sometimes incorrectly. Many other things are also paraphrased incorrectly, or there's simply a typo, or what is said is simply wrong.

    There's no way for others to know what you've read unless you're specific.
     
  8. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #8
    Neat. Using componentsSeparatedByCharactersInSet: might actually avoid the need to distinguish between forward and backwards slashes.

    The only tradeoff is that you end up having to deal with empty strings from adjacent separators
    NSScanner by contrast lets you ignore characters at the beginning of the scan using setCharactersToBeSkipped:

    B
     

Share This Page