Объект arguments
имеет ключи от 0
, поэтому вы должны проходить через этот объект от 0
до keys.length - 1
.
Объект arguments
на самом деле является объектом key-value
, поэтому вы можете использовать оператор in
.
var userEntry1 = Number(window.prompt("Enter in a number of your choice")),
userEntry2 = Number(window.prompt("Enter in a number of your choice")),
userEntry3 = Number(window.prompt("Enter in a number of your choice")),
sum,
i;
function addNumb(userEntry1, userEntry2, userEntry3) {
console.log(arguments)// keys from zero to keys.length - 1
"use strict";
sum = userEntry1 + userEntry2 + userEntry3;
for (var arg in arguments) sum += Number(arguments[arg]);
return sum;
}
addNumb(userEntry1, userEntry2, userEntry3);
console.log(sum);
NSFileManager and NSWorkspace both have file manipulation methods, but NSFileManager's - (BOOL)movePath:(NSString *)source toPath:(NSString *)destination handler:(id)handler
is probably your best bet. Use NSString's path manipulation methods to get the file and folder names right. For example,
NSString *newPath = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newFilename];
[[NSFileManager defaultManager] movePath:oldPath toPath:newPath handler:nil];
Both classes are explained pretty well in the docs, but leave a comment if there's anything you don't understand.
I just wanted to make this easier to understand for a newbie. Here's all the code:
NSString *oldPath = @"/Users/brock/Desktop/OriginalFile.png";
NSString *newFilename = @"NewFileName.png";
NSString *newPath = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newFilename];
[[NSFileManager defaultManager] movePath:oldPath toPath:newPath handler:nil];
NSLog( @"File renamed to %@", newFilename );
Стоит отметить, что переместить файл в сам себя не удастся. У меня был метод, который заменял пробелы символами подчеркивания, делал имя файла строчными буквами и переименовывал файл в новое имя. Файлы, содержащие только одно слово в имени, не смогут переименовать, поскольку новое имя будет идентично в файловой системе без учета регистра.
Я решил эту проблему путем двухэтапного переименования, сначала переименовав файл во временный имя, а затем переименовать его в предполагаемое имя.
Какой-то псевдокод, объясняющий это:
NSString *source = @"/FILE.txt";
NSString *newName = [[source lastPathComponent] lowercaseString];
NSString *target = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newName];
[[NSFileManager defaultManager] movePath:source toPath:target error:nil]; // <-- FAILS
Решение:
NSString *source = @"/FILE.txt";
NSString *newName = [[source lastPathComponent] lowercaseString];
NSString *temp = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-temp", newName]];
NSString *target = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newName];
[[NSFileManager defaultManager] movePath:source toPath:temp error:nil];
[[NSFileManager defaultManager] movePath:temp toPath:target error:nil];