Re: My little file copy/backup program
Re: My little file copy/backup program
- Subject: Re: My little file copy/backup program
- From: Ondra Cada <email@hidden>
- Date: Wed, 22 Mar 2006 17:59:17 +0100
Bobby,
On 22.3.2006, at 17:16, Bobby B wrote:
I'm posting this cause maybe there is one person out there who knows
less Cocoa than I, so it may help them :) If anyone has comments on
why I'm doing something stupidily, please say!
Well if you insist... having just a moment now, why not :)
First, of course, it's pretty disputable whether it makes sense at
all to program something like this for a system where rsync is
available and NSTask has a nice API. Depends: for a real app probably
not, but great for learning :)
-(IBAction) copyAction:(id)sender {
Don't call your selectors "Action". Not that it would harm anything,
but its conventional not to use such suffixes.
Also don't rename it to copy: not to clash with the standard action
for pasteboard :) Use something like copyFiles:, synchronize:, or
whatever on that line...
NSString * srcPath = [NSString stringWithString:[myObj
mySourcePath]];
No need to create a new instance -- in this case, "srcPath=[myObj
mySourcePath]" is quite sufficient. Even if you truly needed a copy,
[[[myObj mySourcePath] copy] autorelease] would be better.
NSDirectoryEnumerator * dirEnum = [[NSFileManager defaultManager]
enumeratorAtPath: srcPath];
Does no harm either, but since you took the pains to put the manager
into an extra variable (fm), why not using it?
while (file = [dirEnum nextObject]) {
Note that in (GNU)C99 you can write this in a more elegant, and,
which is rather more important, less error-prone, way:
for (NSDirectoryEnumerator *dirEnum=[fm
enumeratorAtPath:srcPath];file=[dirEnum nextObject];) ...
Myself, I tend to go as far as to
for (id file,dirEnum=[fm enumeratorAtPath:srcPath];file=[dirEnum
nextObject];) ...
but I have to agree it is inconvenient to lose the compiler help with
strong typing, so this improvement is arguable.
NSString * testStr = [NSString stringWithFormat:@"%@/%
@",destPath,file];
[destPath stringByAppendingPathComponent:file] would be far better.
// This whole enumerator goes through the files that need to be
copied, and makes a string of them,
// to show in the text box, and give to the user as a present, haha.
NSEnumerator * buildUpEnum = [newFiles objectEnumerator];
NSMutableString * buildUpStr = [NSMutableString
stringWithString:@""];
while (file = [buildUpEnum nextObject]) {
[buildUpStr appendString: file];
[buildUpStr appendString:@"\n"];
}
[myObj setMyFilesToCopy:buildUpStr];
Very nice, but [newFiles componentsJoinedByString:@"\n"] would do the
same with lotta less hassle :)
NSMutableString * sourceToCopy = [NSMutableString
stringWithString:@""];
The initialization is not needed here (the object's never used).
Also, it should be plain NSString. And...
while (file = [copyEnum nextObject]) {
sourceToCopy = [NSMutableString stringWithFormat:@"%@/%
@",srcPath,file];
... it should have been rather declared here for better scoping
(again, less error-prone). Same for destToCopy of course :)
// Now that's finished copying..
[myObj setMyFilesToCopy:@"Finished"];
Oh, and of course you don't want to use nonlocalised strings in code :)
---
Ondra Čada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden