我正在尝试创建一个NSTimerin Swift但我遇到了一些麻烦.
NSTimer(timeInterval: 1, target: self, selector: test(), userInfo: nil, repeats: true)
Run Code Online (Sandbox Code Playgroud)
test() 是同一个类中的函数.
我在编辑器中收到错误:
找不到接受提供的参数的'init'的重载
当我改变selector: test()到selector: nil错误消失.
我试过了:
selector: test()selector: testselector: Selector(test())但没有任何作用,我在参考文献中找不到解决方案.
我已经阅读了几篇关于Objective-C方法语法的文章,但我想我不了解一个方法的多个名称.
我正在尝试创建一个名为getBusStopswith NSString和NSTimeIntervalparameters以及返回类型的方法NSMutableArray.这是我构建方法的方法,但它在运行时显然会出错:
- (NSMutableArray *)getBusStops:(NSString *)busStop
(NSTimeInterval *)timeInterval;
Run Code Online (Sandbox Code Playgroud)
我看到了一个方法的另一个例子:
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
Run Code Online (Sandbox Code Playgroud)
我不明白为什么这个方法有每个参数的方法名称.我应该做同样的事情:
- (NSMutableArray *)getBusStops:(NSString *)busStop
forTime:(NSTimeInterval *)timeInterval
Run Code Online (Sandbox Code Playgroud) 我正在尝试下载图像文件并将其保存到照片相册,如下所示
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"Downloading Started");
NSString *urlToDownload = @"http://wallpapercave.com/wp/66iglE0.jpg";
NSURL *url = [NSURL URLWithString:urlToDownload];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"filename.jpg"];
dispatch_async(dispatch_get_main_queue(), ^{
[urlData writeToFile:filePath atomically:YES];
NSLog(@"File Saved !");
NSData *retrievedData = [NSData dataWithContentsOfFile:filePath];
;
dispatch_async(dispatch_get_main_queue(), ^{
UIImageWriteToSavedPhotosAlbum( [UIImage imageWithData:retrievedData],
self,
@selector(done),
NULL); });
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Failed");
});
}
});
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误,
由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[NSInvocation setArgument:atIndex:]:索引(2)超出范围[-1,1]'***第一个抛出调用堆栈:(0x1816751b8 0x1800ac55c 0x18156db24 …