AFNetworking
报错并输出:“
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x8ea19a0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
”原因
AFNetworking默认把响应结果当成json来处理(默认:manager.responseSerializer = [AFJSONResponseSerializer serializer]
) 。
如果返回的数据 并不是一个json文本,而是一个html网页。但是AFNetworking并不知道!然后依旧以json的形式去解析,显然没办法把一个网页解析成一个字典或者数组,所以产生错误。解决
要让它能够正确地处理这个情况,而非提示一个错误。
则须告诉AFNetworking别把这个网页当json来处理!需要在发送请求前加入:manager.responseSerializer = [AFHTTPResponseSerializer serializer]
请求超时的时间限制 设置无效:
-(void)GETUrl:(NSString *)url parameters:(NSDictionary *)parameters success:(void (^)(id))success failure:(void (^)(NSError *))failure { AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone]; //表示不做SSL pinning manager.requestSerializer.timeoutInterval = 6; //请求超时(❌❌❌❌❌❌❌❌) //manager.requestSerializer = [AFHTTPRequestSerializer serializer]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; manager.responseSerializer = [AFJSONResponseSerializer serializer]; manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript",@"text/html",@"text/plain", nil]; [manager GET:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { if (success) { success(responseObject); } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { failure(error); }]; }
超时时间设置的位置 应该放在“
manager.requestSerializer
”设置好之后:-(void)GETUrl:(NSString *)url parameters:(NSDictionary *)parameters success:(void (^)(id))success failure:(void (^)(NSError *))failure { AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone]; //表示不做SSL pinning //manager.requestSerializer = [AFHTTPRequestSerializer serializer]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; manager.responseSerializer = [AFJSONResponseSerializer serializer]; manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript",@"text/html",@"text/plain", nil]; manager.requestSerializer.timeoutInterval = 6; //请求超时 (☑️☑️☑️☑️☑️☑️☑️☑️) [manager GET:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { if (success) { success(responseObject); } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { failure(error); }]; }
或者:
使用KVO:直接在观察属性的变化的过程中,对属性进行修改!
// 设置超时时间(设置有效) [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"]; manager.requestSerializer.timeoutInterval = 30; [manager.requestSerializer didChangeValueForKey:@"timeoutInterval”];
手动实现键值观察
在property的值改变之前调用“willChangeValueForKey:
”,在值改变之后调用“didChangeValueForKey:
”。
FMDB
“No such table xxx” 类型的错误。
原因:
一、关键字 与 插入数据时的关键字 不匹配;
二、数据模型引用 错误。对策:
一、建立数据表时,认真检查;
二、clean一下工程 或 重启Xcode。
“table xxx has no column named xxxxx” 类型的错误
原因:
一、表里确实没有 这个字段。
要在建表的语句中加上 这个字段,另外还要 在所有出现了这个表的语句的地方,都要添加上这个字段。 (表:建立 + 出现)
二、表里之前 没有这个字段,但现在 添加上了。然而,数据库里面已经 存储了 没添加这个字段之前的记录!!如果是这样的话,Xcode会报错崩溃,对策:修改 数据库的版本号 或者 修改 数据库的名称。
IQKeyboardManager
当界面有tableView时,且含有输入框时:
点击输入框,整个界面会集体上移!!效果展示:
点击输入时,界面会整体上移!(导航栏、tableView整体上滑)
解决:
自己对(系统默认的)输入框进行设置 或 自定义一个输入框!!!
使用默认的输入框,进行处理:
- 1.监听键盘在编辑状态的高度,调整界面的约束:上移输入框!!
- 2.输入框 结束编辑状态,调整界面的约束:下移输入框!!
禁用IQKeyboardManager
-(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[IQKeyboardManager sharedManager] setEnable:NO]; [IQKeyboardManager sharedManager].enableAutoToolbar = NO;//显示它自带键盘工具条 开关 } -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [[IQKeyboardManager sharedManager] setEnable:YES]; }
视图创建: (在“
- (void)viewDidLoad { }
”里)self.edgesForExtendedLayout = UIRectEdgeNone; self.discussTabV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain]; self.discussTabV.dataSource = self; self.discussTabV.delegate = self; self.discussTabV.backgroundColor = All_BG_Color; self.discussTabV.bounces = NO; [self.view addSubview:self.discussTabV]; __weak typeof(self) weakSelf = self; [self.discussTabV mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.view); make.right.equalTo(weakSelf.view); make.top.equalTo(weakSelf.view); make.bottom.equalTo(weakSelf.view).with.offset(-SCREEN_WIDTH*100.f/750.f); }]; /** 输入框视图 */ _whiteV = [[UIView alloc] init]; //白色背景视图 _whiteV.backgroundColor = [UIColor whiteColor]; [self.view addSubview:_whiteV]; [_whiteV mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.view); make.right.equalTo(weakSelf.view); make.height.mas_equalTo(SCREEN_WIDTH*100.f/750.f); make.bottom.equalTo(weakSelf.view); }]; _commentTF = [[UITextField alloc] init]; //评论输入框 [_whiteV addSubview:_commentTF]; [_commentTF mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(_whiteV).with.offset(SCREEN_WIDTH*30.f/750.f); make.right.equalTo(_whiteV).with.offset(-SCREEN_WIDTH*30.f/750.f); make.top.equalTo(_whiteV).with.offset(SCREEN_WIDTH*20.f/750.f); make.bottom.equalTo(_whiteV).with.offset(-SCREEN_WIDTH*20.f/750.f); }]; _commentTF.backgroundColor = [UIColor whiteColor]; _commentTF.placeholder = @"我的回答"; _commentTF.clearButtonMode = UITextFieldViewModeWhileEditing; _commentTF.borderStyle = UITextBorderStyleRoundedRect; _commentTF.returnKeyType = UIReturnKeySend; //回车按钮设置为“发送” _commentTF.delegate = self; /** ⭐️⭐️⭐️监听键盘的通知⭐️⭐️⭐️ */ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrameNotify:) name:UIKeyboardWillChangeFrameNotification object:nil];
监听键盘的变化:
/** ⭐️⭐️⭐️当键盘改变了frame(位置、尺寸)时,调用⭐️⭐️⭐️ */ -(void)keyboardWillChangeFrameNotify:(NSNotification*)notify { CGRect keyboardFrame = [notify.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; __weak typeof(self) weakSelf = self; [self.discussTabV mas_updateConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.view); make.right.equalTo(weakSelf.view); make.top.equalTo(weakSelf.view); make.bottom.equalTo(weakSelf.view).with.offset(-keyboardFrame.size.height-SCREEN_WIDTH*100.f/750.f); }]; [_whiteV mas_updateConstraints:^(MASConstraintMaker *make) { //只负责新增约束 make.left.equalTo(weakSelf.view); make.right.equalTo(weakSelf.view); make.height.mas_equalTo(SCREEN_WIDTH*100.f/750.f); make.bottom.equalTo(weakSelf.view).with.offset(-keyboardFrame.size.height); }]; }
输入框结束编辑:
#pragma mark - UITextFieldDelegate -(void)textFieldDidEndEditing:(UITextField *)textField { __weak typeof(self) weakSelf = self; [self.discussTabV mas_updateConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.view); make.right.equalTo(weakSelf.view); make.top.equalTo(weakSelf.view); make.bottom.equalTo(weakSelf.view).with.offset(-SCREEN_WIDTH*100.f/750.f); }]; [_whiteV mas_updateConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.view); make.right.equalTo(weakSelf.view); make.height.mas_equalTo(SCREEN_WIDTH*100.f/750.f); make.bottom.equalTo(weakSelf.view); }]; }
移除通知:
-(void)dealloc { //移除通知 [[NSNotificationCenter defaultCenter] removeObserver:self]; }
效果展示:
点击输入时,界面不会整体上移! 仅仅是输入框配合键盘上移!
参考:
《三方库大全》:各种三方库的介绍!
《三方库的使用》:三方库的使用
《CocoaPods》:CocoaPods介绍和使用
《Cocoapods导入 常用第三方库》:使用Cocoapods导入三方库
文章持续更新中~~
总结的文章全部分开了,以便总结!
网友评论