NSFileManager 里面有这样一个方法: - (BOOL)removeItemAtURL:(NSURL *)URL error:(NSError **)error; 使用的时候我们会传入一个 & 上面那个方法在 Swift 中是这样的: func removeItem(atPath path: String) throws 没有返回值 Objective-C 中为了对外部创建的 NSError 赋值,使用了双指针设计,即 NSError *__autoreleasing*,这种做法在 Swift 语言中,变成了 inout 关键字: func swapTwoInts(_ a: inout Int func removeItem(atPath path: String) throws // 原版 func removeItem(atPath path: String, error: inout NSError 由于 try catch 是一种同步的语法,在异步的时候,我们还是只能通过 Error 或者 NSError 来判断执行是否成功。
andUrlString:(NSString *)urlString andParameters:(id) parameters andFinished:(void(^)(id response, NSError andUrlString:(NSString *)urlString andParameters:(id) parameters andFinished:(void(^)(id response, NSError andUrlString:(NSString *)urlString andParameters:(id) parameters andFinished:(void(^)(id response, NSError *error) = ^(NSURLSessionDataTask *dataTask,NSError *error) { responseBlock(nil, error); *error) = ^(NSURLSessionDataTask *dataTask,NSError *error) { responseBlock(nil, error);
NSData类别中提供的方法如下: //进行数据压缩操作 - (NSData *)dataByGZipCompressingWithError:(NSError * __autoreleasing *) memLevel strategy:(int)strategy error:(NSError * __autoreleasing *)error; //进行数据解压缩操作 - (NSData *)dataByGZipDecompressingDataWithError:(NSError * _ writingContentsToFile:(NSURL *)destinationFile atLevel:(int)level error:(NSError (NSURL *)sourceFile writingContentsToFile:(NSURL *)destinationFile error:(NSError
sharedRecorder] stopRecordingWithHandler:^(RPPreviewViewController * _Nullable previewViewController, NSError }]; } // 开始录屏 - (void)start { [[RPScreenRecorder sharedRecorder] startRecordingWithHandler:^(NSError * _Nullable error))handler; // iOS 10 之后使用 开启录屏 - (void)startRecordingWithHandler:(nullable void(^)(NSError stopRecordingWithHandler:(nullable void(^)(RPPreviewViewController * _Nullable previewViewController, NSError ; // 结束视频流捕获 - (void)stopCaptureWithHandler:(nullable void(^)(NSError * _Nullable error))handler; //
NetworkManager : NSObject- (void)downloadFileWithURL:(NSString *)url completion:(void(^)(NSData *data, NSError targetURL) { completion(nil, [NSError errorWithDomain:@"Invalid URL" code:-1 userInfo:nil]); [filePath isKindOfClass:[NSString class]]) { completion(NO, [NSError errorWithDomain:@"Invalid data) { completion(NO, [NSError errorWithDomain:@"No Data to Save" code:-2 userInfo:nil]); } // 保存文件 [fileSaver saveData:data toPath:filePath completion:^(BOOL success, NSError
; - (nullable instancetype)init; - (nullable instancetype)initWithContentsOfURL:(NSURL *)url error:(NSError )error; - (nullable instancetype)initWithConfiguration:(MLModelConfiguration *)configuration error:(NSError instance of ModelInput to predict from @param error If an error occurs, upon return contains an NSError @param options prediction options @param error If an error occurs, upon return contains an NSError @param options prediction options @param error If an error occurs, upon return contains an NSError
task, id _Nullable responseObject) { // 请求成功 } failure:^(NSURLSessionDataTask * _Nullable task, NSError setQueryStringSerializationWithBlock:(nullable NSString * (^)(NSURLRequest *request, id parameters, NSError )(id <AFMultipartFormData> formData))block error:(NSError *error))failure { // 构建请求实体 NSError *serializationError = nil; NSMutableURLRequest *request *))failure { // 构建请求体 NSError *serializationError = nil; NSMutableURLRequest *request =
*)response data:(nullable NSData *)data error:(NSError * __autoreleasing *)error { BOOL responseIsValid = YES; NSError *validationError = nil; static NSError * AFErrorWithUnderlyingError(NSError *error, NSError *underlyingError) { if (! static BOOL AFErrorOrUnderlyingErrorHasCodeInDomain(NSError *error, NSInteger code, NSString *domain) id responseObject = nil; NSError *serializationError = nil; // Workaround for behavior of Rails to return
在之前的版本中,Swift中Error与OC中NSError没有关系。但是现在两者可以互相强转。 但是我们知道NSError是有UserInfo和domain的。 中,变成Error类型,那么获取NSError中的UserInfo信息也变成了一件头疼的事情,比如AVError: catch let error as NSError where error. 在NSError中也有对应的三个属性: @property (readonly, copy) NSString *localizedDescription; @property (nullable, 成NSError。
}] then:^id _Nullable(id _Nullable value) { return [self check:value]; }] catch:^(NSError @"do_success" : [NSError errorWithDomain:@"learn_promises_do_error" code:-1 userInfo:nil]; }] then @"then_success" : [NSError errorWithDomain:@"learn_promises_then_error" code:-1 userInfo:nil]; }] catch:^(NSError * _Nonnull error) { NSLog(@"error: %@", error); }]; 上面的catch方法表示统一的error fulfill方法则会判断value是否为NSError,如果是NSError则转为reject,否则将状态改为Fulfilled,并且通知observer数组。
task))success failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError ))success failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError )success failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure { //构造一个request请求 NSError *serializationError = nil; NSMutableURLRequest *))failure { //通过请求序列化器构造一个request请求 //需要传入请求方法GET POST DELETE PUT这样的字符串 以及相关参数 NSError
NSManagedObjectContext) { do { try context.save() } catch { let nserror = error as NSError fatalError("Unresolved error \(nserror), \(nserror.userInfo)") newEvent.noteDetail = "" //保存 do { try context.save() } catch { let nserror = error as NSError fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
dataTaskWithRequest:(NSURLRequest*)requestcompletionHandler:(void(^)(NSData*data,NSURLResponse*response,NSError [sessiondataTaskWithURL:urlcompletionHandler:^(NSData*_Nullabledata,NSURLResponse*_Nullableresponse,NSError uploadTaskWithRequest:requestfromFile:filename completionHandler:^(NSData*data,NSURLResponse*response,NSError uploadTaskWithRequest:request fromData:bodycompletionHandler:^(NSData *data, NSURLResponse *response, NSError dataTaskWithRequest:requestcompletionHandler:^(NSData*_Nullabledata,NSURLResponse*_Nullableresponse,NSError
NSError NSError对象中封装了三条错误信息 Error domain(错误范围,其类型为string)一般为:NSCocoaErrorDomain Error code(错误码,类型为int )错误码可以定义为枚举 Error info (用户信息,类型为dictionary)有关此错误的额外信息 - (void)doSomethingWithError:(NSError **)error NSLocalizedFailureReasonErrorKey, @"恢复建议:请创建该文件",NSLocalizedRecoverySuggestionErrorKey,nil]; *error = [NSError errorWithDomain:NSCocoaErrorDomain code:404 userInfo:userInfo]; } NSError *error; [self doSomethingWithError
peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(nullable NSError NA, 7_0); //信号强度改变时调用的方法 - (void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(nullable NSError (NA, 8_0); //发现服务时调用的方法 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError CBPeripheral *)peripheral didDiscoverIncludedServicesForService:(CBService *)service error:(nullable NSError CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(nullable NSError
strongSelf.previewController reloadData]; } failure:^(NSURLSessionDataTask * _Nullable task, NSError Nonnull, id _Nullable))success failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError Nonnull, id _Nullable))success failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError *))failure { NSError *serializationError = nil; NSMutableURLRequest *request = [self.requestSerializer *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorBadURL userInfo:nil];
; - (nullable instancetype)init; - (nullable instancetype)initWithContentsOfURL:(NSURL *)url error:(NSError )error; - (nullable instancetype)initWithConfiguration:(MLModelConfiguration *)configuration error:(NSError @param options prediction options @param error If an error occurs, upon return contains an NSError @param options prediction options @param error If an error occurs, upon return contains an NSError _0字段:举个栗子如下: - (nullable yoloModelOutput *)predictionFromInput__0:(CVPixelBufferRef)input__0 error:(NSError
判断一个数据对象是否可以转化为JSON数据 + (NSData *)dataWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt error:(NSError NSJSONWritingPrettyPrinted = (1UL << 0) } + (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError )writeJSONObject:(id)obj toStream:(NSOutputStream *)stream options:(NSJSONWritingOptions)opt error:(NSError 返回的是写入流的字节数 + (id)JSONObjectWithStream:(NSInputStream *)stream options:(NSJSONReadingOptions)opt error:(NSError
在iOS开发中最常见的方式之一是使用NSError,比如使用很广的AFNetworking,它就会常常返回一些错误信息告诉用户。 而在手Q或者兴趣部落的项目中都没有一套整齐的Error体系,所以我们得需要进一步了解NSerror是什么东西才能制定出我们想要的东西。 NSError包括什么 在整个Error体系中主要包括三块内容,如下: Error Domain Error Code The User Info Dictionary Domain Error Domain + (id)errorWithDomain:(NSString *)domain code:(NSInteger)code userInfo:(NSDictionary *)dict; 总结 在了解到NSError 那么在项目中就需要做到以下几点: 建立属于项目的错误码表 XXXErrors.h 定义属于项目的错误区域 com.company.XXX.ErrorDomain 在一些数据接口或者更底层的一些接口都应该提供NSError
*managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel]; NSError Mapped the article: %@", [result firstObject]); } failure:^(RKObjectRequestOperation *operation, NSError *operationQueue = [NSOperationQueue new]; [operationQueue addOperation:operation]; 把网络请求的错误信息映射一个到 NSError ]; NSLog(@"Mapped the article: %@", article); } failure:^(RKObjectRequestOperation *operation, NSError *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel]; NSError