我的目标是创建一个执行clang格式的扩展.我的代码看起来像这样:
- (void)performCommandWithInvocation:(XCSourceEditorCommandInvocation *)invocation completionHandler:(void (^)(NSError * _Nullable nilOrError))completionHandler
{
NSError *error = nil;
NSURL *executableURL = [[self class] executableURL];
if (!executableURL)
{
NSString *errorDescription = [NSString stringWithFormat:@"Failed to find clang-format. Ensure it is installed at any of these locations\n%@", [[self class] clangFormatUrls]];
completionHandler([NSError errorWithDomain:SourceEditorCommandErrorDomain
code:1
userInfo:@{NSLocalizedDescriptionKey: errorDescription}]);
return;
}
NSMutableArray *args = [NSMutableArray array];
[args addObject:@"-style=LLVM"];
[args addObject:@"someFile.m"];
NSPipe *outputPipe = [NSPipe pipe];
NSPipe *errorPipe = [NSPipe pipe];
NSTask *task = [[NSTask alloc] init];
task.launchPath = executableURL.path;
task.arguments = …Run Code Online (Sandbox Code Playgroud) 我需要在类别的dealloc方法中执行操作.我尝试过调配,但这不起作用(也不是一个好主意).
如果有人问,答案是否定的,我不能使用子类,这是专门针对一个类别的.
我想在延迟时使用[NSTimer scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:]or 执行操作,[self performSelector:withObject:afterDelay:]并在dealloc上取消它.
第一个问题是NSTimer保留了我不想要的目标.[self performSelector:withObject:afterDelay:]不保留,但我需要能够调用[NSObject cancelPreviousPerformRequestsWithTarget:selector:object:]的dealloc方法或我们得到一个崩溃.
有关如何在类别上执行此操作的任何建议吗?
我正在尝试在 viewDidLoad 中获取子视图的绑定大小:(canvas.bounds)
最初我没有使用DispatchQueue.main.async包装器,并且大小没有正确返回。所以通过实验,我将语句包装在主线程队列中。然后它起作用了。
我知道有一个指导方针说“UI相关操作需要放在主线程中”。但这如何转化为实际的编码经验法则:
如果我要更改 UI 属性,例如更改边界大小,是否需要将其包装在主线程队列中?(可能是的,我猜)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// stack views
view.addSubview(photoView)
view.addSubview(canvas)
DispatchQueue.main.async {
self.canvas.startPoint = CGPoint.zero
self.canvas.endPoint = CGPoint(x: self.canvas.bounds.width / 2.0, y: self.canvas.bounds.height)
self.canvas.setNeedsDisplay()
}
Run Code Online (Sandbox Code Playgroud)实际上这引出了另一个我自然想问的问题:必须在 dispatch main 中包装代码看起来“不干净”。如果你必须包装它,是不是意味着“viewDidLoad”不是正确的生命周期使用?应该有一个更合适的生命周期,不需要调度主包装器?
ios ×2
clang-format ×1
cocoa-touch ×1
nstask ×1
nstimer ×1
objective-c ×1
swift ×1
uikit ×1
xcode ×1
xcode8 ×1