iOS笔记(待整理)

文章出处,原创于 https://HawkingOuYang.github.io/

我的GitHub


深拷贝、浅拷贝

Deep copying an NSArray
http://stackoverflow.com/questions/647260/deep-copying-an-nsarray

If you only need a one-level-deep copy, you can explicitly call for one…
NSMutableArray *newArray = [[NSMutableArray alloc] initWithArray:oldArray copyItems:YES];

If you need a true deep copy, such as when you have an array of arrays, you can archive and then unarchive the collection, provided the contents all conform to the NSCoding protocol. An example of this technique is shown in Listing 3.

Listing 3 A true deep copy

NSArray* trueDeepCopyArray = [NSKeyedUnarchiver unarchiveObjectWithData:
[NSKeyedArchiver archivedDataWithRootObject:oldArray]];

{//[begin] fix bug — A true deep copy
/*
http://stackoverflow.com/questions/647260/deep-copying-an-nsarray

If you need a true deep copy, such as when you have an array of arrays, you can archive and then unarchive the collection, provided the contents all conform to the NSCoding protocol. An example of this technique is shown in Listing 3.

Listing 3 A true deep copy

NSArray* trueDeepCopyArray = [NSKeyedUnarchiver unarchiveObjectWithData:
                            [NSKeyedArchiver archivedDataWithRootObject:oldArray]];
*/
ONOXMLElement *deepCopyElement = [NSKeyedUnarchiver unarchiveObjectWithData:
                            [NSKeyedArchiver archivedDataWithRootObject:element]];
//TODO: ONOXMLElement实现了NSCoding协议,unarchiveObjectWithData时 为何崩溃?

self.element = deepCopyElement;//strong reference

}//[end] fix bug — A true deep copy

TODO: 这里还需要一张图片 !!

/ 码字–开始 /
//from - immutable And mutable
NSString str1 = @”str1”;
NSMutableString
mutableStr1 = [NSMutableString stringWithString:@”mutableStr1”];
NSArray arr1 = [ NSArray arrayWithObjects:@”1”,@”2”,@”3”, nil];
NSMutableArray
mutableArr1 = [NSMutableArray arrayWithObjects:@”A”,@”B”,@”C”, nil];
//to - inmutable
NSString copyStr1 = [str1 copy];
NSString
mutableCopyStr1 = [str1 mutableCopy];
NSString copyMutableStr1 = [mutableStr1 copy];
NSString
mutablecopyMutableStr1 = [mutableStr1 mutableCopy];
//to - mutable
NSMutableString copyStr1ToM = [str1 copy];
NSMutableString
mutableCopyStr1ToM = [str1 mutableCopy];
NSMutableString copyMutableStr1ToM = [mutableStr1 copy];
NSMutableString
mutablecopyMutableStrToM = [mutableStr1 mutableCopy];

//log
NSLog(@”copyStr1=%p,%@—-from—str1=%p,%@”,
copyStr1,copyStr1, str1,str1);
NSLog(@”mutableCopyStr1=%p,%@————from—str1=%p,%@”,
mutableCopyStr1,mutableCopyStr1, str1,str1);
NSLog(@”copyMutableStr1=%p,%@————from—mutableStr1=%p,%@”,
copyMutableStr1,copyMutableStr1, mutableStr1,mutableStr1);
NSLog(@”mutablecopyMutableStr1=%p,%@————–from—mutableStr1=%p,%@”,
mutablecopyMutableStr1,mutablecopyMutableStr1, mutableStr1,mutableStr1);
NSLog(@”copyStr1ToM=%p,%@———from—str1=%p,%@”,
copyStr1ToM,copyStr1ToM, str1,str1);
NSLog(@”mutableCopyStr1ToM=%p,%@—————from—str1=%p,%@”,
mutableCopyStr1ToM,mutableCopyStr1ToM, str1,str1);
NSLog(@”copyMutableStr1ToM=%p,%@—————from—mutableStr1=%p,%@”,
copyMutableStr1ToM,copyMutableStr1ToM, mutableStr1,mutableStr1);
NSLog(@”mutablecopyMutableStrToM=%p,%@———————from–mutableStr1=%p,%@”,
mutablecopyMutableStrToM,mutablecopyMutableStrToM, mutableStr1,mutableStr1);
/ 码字–好累 /

2014-11-27 14:02:20.437 TestCopy[466:6731] copyStr1=0x100001088,str1—-from—str1=0x100001088,str1
2014-11-27 14:02:20.439 TestCopy[466:6731] mutableCopyStr1=0x100206420,str1————from—str1=0x100001088,str1
2014-11-27 14:02:20.440 TestCopy[466:6731] copyMutableStr1=0x100206500,mutableStr1————from—mutableStr1=0x100204d80,mutableStr1
2014-11-27 14:02:20.440 TestCopy[466:6731] mutablecopyMutableStr1=0x100206520,mutableStr1————–from—mutableStr1=0x100204d80,mutableStr1
2014-11-27 14:02:20.441 TestCopy[466:6731] copyStr1ToM=0x100001088,str1———from—str1=0x100001088,str1
2014-11-27 14:02:20.441 TestCopy[466:6731] mutableCopyStr1ToM=0x100206580,str1—————from—str1=0x100001088,str1
2014-11-27 14:02:20.441 TestCopy[466:6731] copyMutableStr1ToM=0x1002065e0,mutableStr1—————from—mutableStr1=0x100204d80,mutableStr1
2014-11-27 14:02:20.441 TestCopy[466:6731] mutablecopyMutableStrToM=0x100206600,mutableStr1———————from–mutableStr1=0x100204d80,mutableStr1
Program ended with exit code: 0

app 修改hosts

#pragma mark - 同步联系人
/*
因为:直接访问测试地址(即ip地址)不行;
那么:需要修改本地hosts(即设置每个request实例的host);
这么做:

#if defined( DEBUG )
[request setValue:@”example.domain.com” forHTTPHeaderField:@”host”];//修改本地hosts

#endif
所以,这样,客户端使用域名访问
*/

//! 同步联系人,基于 WebDAV 协议

#if defined( DEBUG )
static NSString const kBaseURL_syncContact = @”http://255.255.255.100/“; //测试环境
static NSString
const kURL_syncContactInfo = @”sync/contact”;

#else
static NSString const kBaseURL_syncContact = @”http://example.domain.com/“; //生产环境
static NSString
const kURL_syncContactInfo = @”sync/contact”;

#endif

#if defined( DEBUG )
// TODO: test code
[request setValue:@”example.domain.com” forHTTPHeaderField:@”host”];

#endif

tableView 性能优化

Cell圆角

方式一:layer 设置圆形,重绘

1
2
_headImageView.layer.shouldRasterize = YES;
_headImageView.layer.rasterizationScale = [UIScreen mainScreen].scale;

这个不太靠谱。

方式二:圆形 layer,填充图片

Fastest way to do shadows on iOS?

Performance scrolling and retina resolution images in CALayer

bharath2020/UITableViewTricks 这个很好!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//用CALayer实现圆角。
//! 头像,用CALayer实现圆角。
- (CALayer *)mHeadImgLayer
{
if (!_mHeadImgLayer) {
_mHeadImgLayer = [CALayer new];
_mHeadImgLayer.frame = CGRectMake(0, 0, R(54.f-14), R(54.f-14));
_mHeadImgLayer.cornerRadius = (R(54.f-14)) / 2;
_mHeadImgLayer.masksToBounds = YES;
_mHeadImgLayer.shouldRasterize = YES;
_mHeadImgLayer.rasterizationScale = [UIScreen mainScreen].scale;
}
return _mHeadImgLayer;
}
- (void)layoutSubviews
{
[super layoutSubviews];
_mHeadImgLayer.frame = _headImgV.frame;
_mStrangerImgLayer.frame = _headImgV.frame;
}
_headImgV.hidden = YES;
[self.contentView.layer addSublayer: [self mHeadImgLayer]];
[self.contentView.layer addSublayer: [self mStrangerImgLayer]];
[CATransaction begin];
[CATransaction setAnimationDuration:0];
_mHeadImgLayer.contents = (id)aPlaceholderImage.CGImage;
[CATransaction commit];
- (UIImageView *)strangerHeadImgV
{
if (!_strangerHeadImgV) {
_strangerHeadImgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, R(54.f-14), R(54.f-14))];
_strangerHeadImgV.contentMode = UIViewContentModeScaleAspectFill;
_strangerHeadImgV.layer.masksToBounds = YES;
_strangerHeadImgV.layer.cornerRadius = (R(54.f-14)) / 2;
}
return _strangerHeadImgV;
}
cell搜索匹配高亮
cell动态高度
cell动态内容
table批更新

tableView batch update

How to detect that animation has ended on UITableView beginUpdates/endUpdates? (这个 好呀)

Batch updates for UITableView and UICollectionView (这个 好呀)

TableView reloadData vs. beginUpdates & endUpdates

layoutSubviews

When does layoutSubviews get called?

It’s important to optimize any UIView layoutSubviews method you create, as it can be frequently called, and has the potential for creating recursion (triggering a setNeedsLayout from layoutSubviews can create a loop that will grossly affect your apps performance). Layout subviews is called once per run loop on any view that has had setNeedsLayout or setNeedsDisplayWithRect: called on it. So in addition to any time you manually call these methods, it can be useful to know when the UI framework calls setNeedsLayout/setNeedsDisplay as this will trigger layoutSubviews.

For this purpose, I will define a few view relationships:

  • View1 – UIView class, root view for examples
  • View1.1 – UIScrollView class, subview of View1
  • View1.1.1 – UIView class, subview of View1.1 (No autoresize mask)
  • View1.1.2 – UIView class, another subview of View1.1 (Autoresize mask – flexible width)

I then ran the following tests. An X means the view was layed out

//TODO: layoutSubviews 图片

From this I surmise the following:

  • init does not cause layoutSubviews to be called (duh)
  • addSubview causes layoutSubviews to be called on the view being added, the view it’s being added to (target view), and all the subviews of the target view
  • setFrame intelligently calls layoutSubviews on the view having it’s frame set only if the size parameter of the frame is different
  • scrolling a UIScrollView causes layoutSubviews to be called on the scrollView, and it’s superview
  • rotating a device only calls layoutSubview on the parent view (the responding viewControllers primary view)
  • removeFromSuperview – layoutSubviews is called on superview only (not show in table)

Hopefully this is helpful information for you as well.

英文:When is layoutSubviews called?
http://stackoverflow.com/questions/728372/when-is-layoutsubviews-called

中文:什么时候调用layoutSubviews
http://blog.csdn.net/hopedark/article/details/24313445

I had a similar question, but wasn’t satisfied with the answer (or any I could find on the net), so I tried it in practice and here is what I got:

  • init does not cause layoutSubviews to be called (duh)
  • addSubview: causes layoutSubviews to be called on the view being added, the view it’s being added to (target view), and all the subviews of the target
  • view setFrame intelligently calls layoutSubviews on the view having its frame set only if the size parameter of the frame is different
  • scrolling a UIScrollView causes layoutSubviews to be called on the scrollView, and its superview
  • rotating a device only calls layoutSubview on the parent view (the responding viewControllers primary view)
  • Resizing a view will call layoutSubviews on its superview

今天在写程序时候遇见layoutSubviews触发时候引起的问题。特来总结一下什么时候会触发layoutSubviews:

layoutSubviews在以下情况下会被调用:

1、init初始化不会触发layoutSubviews

2、addSubview会触发layoutSubviews

3、设置view的Frame会触发layoutSubviews,当然前提是frame的值设置前后发生了变化

4、滚动一个UIScrollView会触发layoutSubviews

5、旋转Screen会触发父UIView上的layoutSubviews事件

6、改变一个UIView大小的时候也会触发父UIView上的layoutSubviews事件

网络回调

What exactly is RESTful programming?

网络回调方式总结:

1、代理
2、通知
3、代码块
4、IMP

代理
优点:效率高
缺点:代码耦合度高,若一个Controller有多个网络请求,响应同一个代理方法,代码将变得臃肿难看

通知
优点:代码耦合度低,简单
缺点:多对多,容易滥用,不易控制,释放不及时可能导致通知方法调用多次,不易维护

代码块
优点:耦合度低,效率较高,代码更优雅
缺点:块会延迟对象的生命周期,占用较多的内存,使用不当会造成内存泄露,以及循环引用

IMP
优点:耦合度低
缺点:编译时不会错误检查(如方法名错误等),不易维护

iOS

Why isn’t ProjectName-Prefix.pch created automatically in Xcode 6?

//TODO: 插入图片

1
2
Well, depending on the working dir, variables could vary. I am using$(PRODUCT_DIR)/$(PRODUCT_NAME)/PrefixHeader.pch – Alejandro Iván Oct 13 '15 at 14:40
If you keep the .pch file within the particular folder, then the situation occurred. – Prakash Raj Oct 15 '15 at 6:22

UIScrollView 实践经验

Charles 从入门到精通 - 唐巧的技术博客

iphone中自定义Universal Framework

Python in Xcode 7

Tips:使用Xcode查找项目中的中文字符串

XMPP协议实现原理介绍

Xcode 5: Test UITableView with XCTest framework

【原】ios打包ipa的四种实用方法(.app转.ipa)

AFNetworking使用

AFNetwork 作用和用法详解

AFNetworking 3 批量上传图片的 3 种方法

dispatch_group_notify does not wait for one dispatch_group_async (这个好)

Getting Download progress with AFNetworking 2.0 + NSProgress + Custom ProgressView

AFHTTPRequestOperationManager post multi-part request not working (这个好呀)

How to set HTTP request body using AFNetwork’s AFHTTPRequestOperationManager?

总结-AFNetworking学习笔记

send image along with other parameters with AFNetworking

AFNetworking 2: How to cancel a AFHTTPRequestOperationManager request?

AFNetworking 2.0 // How to read response header

网络2-AFNetworking和WebView

Afnetworking 2 with parameters

用React Native开发第一个iOS应用

Swift语言iOS开发:CALayer十则示例

使用Flurry来统计和分析用户行为

RestKit

cocoa-rest-client

iOS图片加载速度极限优化—FastImageCache解析

Creating iOS/OSX Frameworks: is it necessary to codesign them before distributing to other developers?

What is the difference between NS_ENUM and NS_OPTIONS?

Linux (Mac 类Unix)

Why is FreeBSD deprecating GCC in favor of Clang/LLVM?

GCC,LLVM,Clang编译器对比

Clang vs GCC for my Linux Development project

LLVM vs. GCC for iOS development

What features does Darwin have that other Unixes don’t, or vice versa?

Where does Mac OS X come from?

How close are Mac OS X and BSD related?

multi-CPU, multi-core, and hyper-thread

SSH with Public Key-Based Authentication

安装macvim

安装Homebrew:打开“终端”程序,输入

1
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)",搞定

在mac上安装macvim

MacVim安装与配置

MacVim学习总结

gem brew ruby pod

Operation not permitted - /usr/bin/update_rubygems

关键:sudo gem install -n /usr/local/bin GEM_NAME_HERE

/usr/bin vs /usr/local/bin on Linux (只关注: /usr/local/bin 即可)

Homebrew 是 The missing package manager for OS X,

Homebrew installs packages to their own directory and then symlinks their files into /usr/local.

如何快速正确的安装 Ruby, Rails 运行环境