Captureing ‘self’ strongly in this block is likely to lead to a retain cycle

警告:Captureing ‘selfstrongly in this block is likely to lead to a retain cycle

这个警告我们在使用block时会经常碰到,什么原因引起的呢?其实是ARC机制造成的

分析:Block对象在创建时会retain一次,如果在block中使用self.xxx时,ARC会自动将self也retain一次,这样就形成了一个retain cycle

解决:创建一个本地变量blockSelf,指向self,然后用结构体语法访问实例变量。

如:__block ViewController *blockSelf = self;[……]

继续阅读

iOS WebView的用法

一、UIWebView 可以加载和显示某个URL的网页,也可以显示基于HTML的本地网页或部分网页:
a. 加载 URL

1
2
3
4
WebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 44, 320, 400)];   
NSString *path = @"http://www.baidu.com";   
NSURL *url = [NSURL URLWithString:path];   
[WebView loadRequest:[NSURLRequest requestWithURL:url]];

b. 加载 HTML

1
2
3
4
5
NSBundle *bundle = [NSBundle mainBundle];
NSString *resPath = [bundle resourcePath];
NSString *filePath = [resPath stringByAppendingPathComponent:@"Home.html"];
[webView loadHTMLString:[NSString stringWithContentsOfFile:filePath]
  baseURL:[NSURL fileURLWithPath:[bundle bundlePath]]];

二、使用网页加载指示,加载完成后再显示网页出来
首先要指定委托方法:
webView.delegate =self;
UIWebView主要有下面几个委托方法:

1
2
3
- (void)webViewDidStartLoad:(UIWebView *)webView;开始加载的时候执行该方法。
- (void)webViewDidFinishLoad:(UIWebView *)webView;加载完成的时候执行该方法。
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;加载出错的时候执行该方法。

这样,可以利用 webViewDidStartLoad 和 webViewDidFinishLoad 方法实现本功能:[……]

继续阅读

AFNetwork 2.0在请求时报错code=-1016 和 3840

在进行网络请求时出现-1016 是因为只支持text/json,application/json,text/javascript
你可以添加text/html,一劳永逸的方法是在AFURLResponseSerialization.h里面搜索self.acceptableContentTypes然后在里面添加
@”text/html”,@”text/plain”这样就可以解决-1016的错误了但是随之而来的是3840错误

Error Domain=NSCocoaErrorDomain Code=3840 “The operation couldn’t be completed. (Cocoa[……]

继续阅读

AFNetworking无法发送Web请求Code = -1016

2014-09-02 15:02:52.601 Crayfish[7211:60b] Error Domain=com.alamofire.error.serialization.response Code=-1016 “Request failed: unacceptable content-type: text/html”

从以上的错误提示来看问题已经明了了,unacceptable content-type: text/html“无法接受的内容类型“text/html”
解决办法:
找到AFURLResponseSerialization.m文件中的init方法

1
- (instance[......]<p class="read-more"><a href="https://www.yusian.com/blog/analysis/2014/09/02/150803532.html">继续阅读</a></p>

Attempt to set a non-property-list object as an NSUserDefaults value for key

2014-09-01 15:55:54.703 RouteMapInformation[26443:60b] Attempt to set a non-property-list object (“\U4e00\U952e\U627e\U8f66\Uff0c\U65b9\U4fbf\U5feb\U6377\Uff0c\U7701\U65f6\U7701\U529b”,””
) as an NSUserDefaults value for key RMGuideView

出现这种情况的根本原因是什么,其实最后一行有关键字“NSUserDefaults”,NSUserDefaults 不能用来存[……]

继续阅读