当前位置: 首页 > news >正文

全国造价工程师管理系统官网温州网站建设选择乐云seo

全国造价工程师管理系统官网,温州网站建设选择乐云seo,高校专业建设五大要素,网站建设用什么技术这次来说一下AFURLResponseSerialization这个HTTP响应类。 定义了一个协议#xff0c;该协议返回序列化后的结果。后续的AFHTTPResponseSerializer以及他的子类都遵循了该协议 该类内有很多子类定义#xff0c;这里借用一张图来展示#xff0c;之后一个一个来说。 我们先来看… 这次来说一下AFURLResponseSerialization这个HTTP响应类。 定义了一个协议该协议返回序列化后的结果。后续的AFHTTPResponseSerializer以及他的子类都遵循了该协议 该类内有很多子类定义这里借用一张图来展示之后一个一个来说。 我们先来看下AFHTTPResponseSerializer在头文件中的定义有哪些。 可以看到有两个初始化方法然后stringEncoding后面有一个宏这个宏代表这弃用了。    来看实现文件的部分。 这里有一个NSIndexSet。 NSIndexSet代表一个不可变的独特的无符号整数的集合,称为索引,因为使用它们的方式。这个集合被称为索引集。你不应该使用索引集存储任意集合的整数值,因为索引集按索引排序范围的方式存储的。这使得它们更有效率比存储单个整数的集合。这也意味着每个索引值指数中只能出现一次。通俗点讲NSIndexSet就是一个唯一的有序的无符号整数的集合。 self.acceptableStatusCodes [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 100)]; 表示接受状态码范围200~299。 - (BOOL)validateResponse:(NSHTTPURLResponse *)responsedata:(NSData *)dataerror:(NSError * __autoreleasing *)error {BOOL responseIsValid YES;//默认为yesNSError *validationError nil;//存在response且为NSHTTPURLResponse类if (response [response isKindOfClass:[NSHTTPURLResponse class]]) {//条件:1.acceptableContentTypes存在 2.response的MIMEType不在范围内 3.MIMEType和data均不为空if (self.acceptableContentTypes ![self.acceptableContentTypes containsObject:[response MIMEType]] !([response MIMEType] nil [data length] 0)) {//data长度大于0且respone的URL存在if ([data length] 0 [response URL]) {//定义错误信息字典NSMutableDictionary *mutableUserInfo [{NSLocalizedDescriptionKey: [NSString stringWithFormat:NSLocalizedStringFromTable(Request failed: unacceptable content-type: %, AFNetworking, nil), [response MIMEType]],NSURLErrorFailingURLErrorKey:[response URL],AFNetworkingOperationFailingURLResponseErrorKey: response,} mutableCopy];if (data) {//错误信息包含datamutableUserInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] data;}//生成NSerrorvalidationError AFErrorWithUnderlyingError([NSError errorWithDomain:AFURLResponseSerializationErrorDomain code:NSURLErrorCannotDecodeContentData userInfo:mutableUserInfo], validationError);}responseIsValid NO;}if (self.acceptableStatusCodes ![self.acceptableStatusCodes containsIndex:(NSUInteger)response.statusCode] [response URL]) {NSMutableDictionary *mutableUserInfo [{NSLocalizedDescriptionKey: [NSString stringWithFormat:NSLocalizedStringFromTable(Request failed: % (%ld), AFNetworking, nil), [NSHTTPURLResponse localizedStringForStatusCode:response.statusCode], (long)response.statusCode],NSURLErrorFailingURLErrorKey:[response URL],AFNetworkingOperationFailingURLResponseErrorKey: response,} mutableCopy];if (data) {mutableUserInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] data;}validationError AFErrorWithUnderlyingError([NSError errorWithDomain:AFURLResponseSerializationErrorDomain code:NSURLErrorBadServerResponse userInfo:mutableUserInfo], validationError);responseIsValid NO;}}if (error !responseIsValid) {*error validationError;}return responseIsValid; } response有效性验证过程。 NSSecureCoding, NSCopying协议方法。   进入该属性类型NSJSONReadingOptions可以看到是下图这么定义的。 NSJSONReadingMutableContainers返回是可变容器类型如NSMutableArrayNSMutableDictionay。 NSJSONReadingMutableLeaves返回的字符串是NSMutableSting。 NSJSONReadingAllowFragmentsjson结构最外层可以不是NSArray和NSDictionay。   接着来看对应的实现文件部分。 三种初始化方法从init方法中可以看到acceptableContentTypes支持application/json, text/json, text/javascript三种。 - (id)responseObjectForResponse:(NSURLResponse *)responsedata:(NSData *)dataerror:(NSError *__autoreleasing *)error {//response有效性无效if (![self validateResponse:(NSHTTPURLResponse *)response data:data error:error]) {//无error或者code为NSURLErrorCannotDecodeContentData解析无效要返回nilif (!error || AFErrorOrUnderlyingErrorHasCodeInDomain(*error, NSURLErrorCannotDecodeContentData, AFURLResponseSerializationErrorDomain)) {return nil;}}// Workaround for behavior of Rails to return a single space for head :ok (a workaround for a bug in Safari), which is not interpreted as valid input by NSJSONSerialization.// See https://github.com/rails/rails/issues/1742//data由空格字符构成也需要返回nilBOOL isSpace [data isEqualToData:[NSData dataWithBytes: length:1]];if (data.length 0 || isSpace) {return nil;}NSError *serializationError nil;id responseObject [NSJSONSerialization JSONObjectWithData:data options:self.readingOptions error:serializationError];if (!responseObject){if (error) {*error AFErrorWithUnderlyingError(serializationError, *error);}return nil;}if (self.removesKeysWithNullValues) {return AFJSONObjectByRemovingKeysWithNullValues(responseObject, self.readingOptions);}return responseObject; } 转化和解析方法这里又出现了两个定义的C函数贴在下面。 static BOOL AFErrorOrUnderlyingErrorHasCodeInDomain(NSError *error, NSInteger code, NSString *domain) {//error的domain和code与入参对影响等直接返回yesif ([error.domain isEqualToString:domain] error.code code) {return YES;} else if (error.userInfo[NSUnderlyingErrorKey]) {//否则判断userInfo中key为NSUnderlyingErrorKey是否有值有递归调用自身return AFErrorOrUnderlyingErrorHasCodeInDomain(error.userInfo[NSUnderlyingErrorKey], code, domain);}return NO; } static id AFJSONObjectByRemovingKeysWithNullValues(id JSONObject, NSJSONReadingOptions readingOptions) {//数组类if ([JSONObject isKindOfClass:[NSArray class]]) {//定义可变数组NSMutableArray *mutableArray [NSMutableArray arrayWithCapacity:[(NSArray *)JSONObject count]];for (id value in (NSArray *)JSONObject) {[mutableArray addObject:AFJSONObjectByRemovingKeysWithNullValues(value, readingOptions)];}return (readingOptions NSJSONReadingMutableContainers) ? mutableArray : [NSArray arrayWithArray:mutableArray];} else if ([JSONObject isKindOfClass:[NSDictionary class]]) {//同上NSMutableDictionary *mutableDictionary [NSMutableDictionary dictionaryWithDictionary:JSONObject];for (id NSCopying key in [(NSDictionary *)JSONObject allKeys]) {id value (NSDictionary *)JSONObject[key]; //去除value为NSNull的keyif (!value || [value isEqual:[NSNull null]]) {[mutableDictionary removeObjectForKey:key];} else if ([value isKindOfClass:[NSArray class]] || [value isKindOfClass:[NSDictionary class]]) {mutableDictionary[key] AFJSONObjectByRemovingKeysWithNullValues(value, readingOptions);}}return (readingOptions NSJSONReadingMutableContainers) ? mutableDictionary : [NSDictionary dictionaryWithDictionary:mutableDictionary];}return JSONObject; }  这里有点好奇为什么不把数组里面的NSNull对象也去掉而只是把字典中value为NSNull的key去掉。 NSSecureCoding, NSCopying协议方法。   解析XML的子类。 不多说比较好理解。   将JSON转换为PropertyList用到的子类。 ContentType支持application/x-plist。   这里是UIImage的一个分类从方法名字上看可以想到是一个安全的NSData转Image的方法。 通过NSLock来解决多线程上的一些安全问题。 static UIImage * AFImageWithDataAtScale(NSData *data, CGFloat scale) {UIImage *image [UIImage af_safeImageWithData:data];if (image.images) {return image;}return [[UIImage alloc] initWithCGImage:[image CGImage] scale:scale orientation:image.imageOrientation]; }static UIImage * AFInflatedImageFromResponseWithDataAtScale(NSHTTPURLResponse *response, NSData *data, CGFloat scale) {if (!data || [data length] 0) {return nil;}CGImageRef imageRef NULL;CGDataProviderRef dataProvider CGDataProviderCreateWithCFData((__bridge CFDataRef)data);if ([response.MIMEType isEqualToString:image/png]) {imageRef CGImageCreateWithPNGDataProvider(dataProvider, NULL, true, kCGRenderingIntentDefault);} else if ([response.MIMEType isEqualToString:image/jpeg]) {imageRef CGImageCreateWithJPEGDataProvider(dataProvider, NULL, true, kCGRenderingIntentDefault);if (imageRef) {CGColorSpaceRef imageColorSpace CGImageGetColorSpace(imageRef);CGColorSpaceModel imageColorSpaceModel CGColorSpaceGetModel(imageColorSpace);// CGImageCreateWithJPEGDataProvider does not properly handle CMKY, so fall back to AFImageWithDataAtScaleif (imageColorSpaceModel kCGColorSpaceModelCMYK) {CGImageRelease(imageRef);imageRef NULL;}}}CGDataProviderRelease(dataProvider);UIImage *image AFImageWithDataAtScale(data, scale);if (!imageRef) {if (image.images || !image) {return image;}imageRef CGImageCreateCopy([image CGImage]);if (!imageRef) {return nil;}}size_t width CGImageGetWidth(imageRef);size_t height CGImageGetHeight(imageRef);size_t bitsPerComponent CGImageGetBitsPerComponent(imageRef);if (width * height 1024 * 1024 || bitsPerComponent 8) {CGImageRelease(imageRef);return image;}// CGImageGetBytesPerRow() calculates incorrectly in iOS 5.0, so defer to CGBitmapContextCreatesize_t bytesPerRow 0;CGColorSpaceRef colorSpace CGColorSpaceCreateDeviceRGB();CGColorSpaceModel colorSpaceModel CGColorSpaceGetModel(colorSpace);CGBitmapInfo bitmapInfo CGImageGetBitmapInfo(imageRef);if (colorSpaceModel kCGColorSpaceModelRGB) {uint32_t alpha (bitmapInfo kCGBitmapAlphaInfoMask); #pragma clang diagnostic push #pragma clang diagnostic ignored -Wassign-enumif (alpha kCGImageAlphaNone) {bitmapInfo ~kCGBitmapAlphaInfoMask;bitmapInfo | kCGImageAlphaNoneSkipFirst;} else if (!(alpha kCGImageAlphaNoneSkipFirst || alpha kCGImageAlphaNoneSkipLast)) {bitmapInfo ~kCGBitmapAlphaInfoMask;bitmapInfo | kCGImageAlphaPremultipliedFirst;} #pragma clang diagnostic pop}CGContextRef context CGBitmapContextCreate(NULL, width, height, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo);CGColorSpaceRelease(colorSpace);if (!context) {CGImageRelease(imageRef);return image;}CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, width, height), imageRef);CGImageRef inflatedImageRef CGBitmapContextCreateImage(context);CGContextRelease(context);UIImage *inflatedImage [[UIImage alloc] initWithCGImage:inflatedImageRef scale:scale orientation:image.imageOrientation];CGImageRelease(inflatedImageRef);CGImageRelease(imageRef);return inflatedImage; } 上面的这些个方法涉及图形图像和CoreGraphics不是很懂。。以后再补吧先把代码贴在这里。       最后还有个AFCompoundResponseSerializer也比较简单不常用不多说了。   通过该篇记录下之后需要学习的东西 CoreGraphicsNSError 转载于:https://www.cnblogs.com/kaisi/p/9442724.html
http://www.zqtcl.cn/news/903859/

相关文章:

  • 龙岗建设高端网站如何建立网站会员系统吗
  • 中国建设银行的网站色彩wordpress 图片采集器
  • 渭南做网站价格江西省城乡住房建设部网站
  • 个人网站可以做充值安徽建设厅网站首页
  • 技术支持 东莞网站建设石材小企业网站建设查询
  • 政务公开网站建设的亮点和建议wordpress注册怎么设置密码
  • 外贸有哪些网站成都网络营销搜索推广优势
  • 国外mod大型网站财税公司
  • 一个很好的个人网站开发做一个简单网页多少钱
  • 东莞在哪里学网站建设网站建设团队与分工
  • 网站功能插件昆明网站建设技术研发中心
  • 网站开发培训中心 市桥移动端ui
  • 高碑店地区网站建设上海排名十大装潢公司
  • 无锡自助建站网站还是新能源专业好
  • pc 手机网站 微站如何建设与维护网站
  • 大学生兼职网站开发毕设论文杭州网络排名优化
  • 做教育机器网站网站建设的步骤图
  • 桔子建站是什么平台郑州公司注册网上核名
  • 网站开发技能有哪些网站建设艾金手指科杰
  • 网站建设挂什么费用网站建设学那些课
  • 网站定位与功能分析在互联网公司做网站
  • 安阳网站建设兼职做网站推广有哪些公司
  • 网站制作的一般过程怎么用手机搭建网站
  • 备案 网站名称 怎么改深圳建网站公司
  • html 企业网站模板网站策划书免费
  • 网站建设销售ppt拖拽建站系统源码
  • 网站托管费用多少网站的开发流程
  • 周到的商城网站建设北京品牌网站
  • 网站开发费用属于什么科目网站建设考试多选题
  • c asp做网站wordpress4.5.2文章采集