我对块有点新,需要一些帮助.我想在一个块中存储一个块,NSDictionary并在它被访问时执行所述块key.这是我迄今为止的一个例子.
NSDictionary *blocks = [NSDictionary dictionaryWithObjectsAndKeys:
^{NSLog(@"Log Block 1");}, @"Block1",
^{NSLog(@"Log Block 2");}, @"Block2",
nil];
Run Code Online (Sandbox Code Playgroud)
然后我通过使用来列举字典keyEnumerator.我知道块被正确存储,因为我NSLog(@"%@", [blocks objectForKey:key]);在枚举期间调用并获取<__NSGlobalBlock__: 0x100003750>.所以我知道我可以访问它,但是我怎么能在这一点上执行呢?
我无法在 Google 上找到答案。我正在 Cocoa 中构建命令行实用程序,当我尝试创建一个实例时,NSWorkspace出现编译器错误。这是我在 中实现的代码main.m,非常简单:
NSArray *runningApps = [[NSWorkspace sharedWorkspace] runningApplications];
for (NSRunningApplication *app in runningApps) {
if (![app terminate])
[app forceTerminate];
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译和运行程序时,我得到的是:
Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_NSWorkspace", referenced from:
objc-class-ref in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)
我在其他应用程序中使用了相同的代码并且没有问题。我知道NSWorkspace是定义的,<Foundation/Foundation.h>并且我已经<Cocoa/Cocoa.h>导入了涵盖 Foundation 的内容。关于问题可能是什么的任何想法?
我试图NSFileManager copyItemAtURL:toURL:error:将UNIX可执行文件(命令行程序)从一个目录移动到另一个目录,但我总是收到一个错误,指出URL类型不受支持.我假设这是因为没有文件扩展名它被视为目录但我不确定.是否可以移动此类文件NSFileManager?
编辑:这是我的代码
#define SAVE_DIR [@"~/Library/Prog" stringByExpandingTildeInPath]
#define PROG_PATH [SAVE_DIR stringByAppendingString:@"/ProgCom"]
#define RESOURCES [[NSBundle mainBundle] resourcePath]
#define LOCAL_PROG [RESOURCES stringByAppendingString:@"/ProgCom"]
-(void)moveProg
{
NSError *error = nil;
NSURL *fromURL = [NSURL URLWithString:LOCAL_PROG];
NSURL *toURL = [NSURL URLWithString:PROG_PATH];
NSLog(@"%@", [fromURL path]);
NSLog(@"%@", [toURL path]);
if ([fMan fileExistsAtPath:[fromURL path]]) {
[fMan copyItemAtURL:fromURL
toURL:toURL
error:&error];
if (error)
[NSApp presentError:error];
}
}
Run Code Online (Sandbox Code Playgroud)
我收到的错误:
The file couldn't be opened because the specified URL type isn't supported.
最后记录了什么:
fromURL = /Users/Nick/Library/Developer/Xcode/DerivedData/Prog-dpnblqaraeuecyadjgizbinfrtcm/Build/Products/Debug/Prog.app/Contents/Resources/ProgCom
toURL = …Run Code Online (Sandbox Code Playgroud)