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

drharv

macrumors newbie
Original poster
Jul 18, 2008
5
0
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;
}
 
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
 
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.
 
I was going to say 'while (string contains "\\") replace "\\" with "\"', but I didn't know what method to use for the conditional part.

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
 
Last edited:
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.
 
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).
 
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.

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.
 
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).

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
Adjacent occurrences of the separator characters produce empty strings in the result. Similarly, if the string begins or ends with separator characters, the first or last substring, respectively, is empty.

NSScanner by contrast lets you ignore characters at the beginning of the scan using setCharactersToBeSkipped:

B
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.