1.Block中的使用多个全局实例怎么办
你可以这样
__weak typeof(SelectView)weakSelectDanJia = SelectView;
__weak typeof(SelectView1)weakAllNumber = SelectView1;
__weak typeof(SelectView2)weakMoney = SelectView2;
__weak typeof(_numberTF)weakNumber = _numberTF;
__weak typeof(self)weakSelf = self;
_numberTF.block = ^(NSInteger cellTag) {
if (!weakSelectDanJia.selectTitle) {
weakNumber.text = @"";
[MBProgressHUD showError:@"请先选择货号"];
}else{
if (weakNumber.text.length>0) {
NSInteger allNumber = weakSelf.fhsrate*[weakNumber.text integerValue];
CGFloat allmoney = [weakSelectDanJia.selectTitle doubleValue]*allNumber;
weakAllNumber.selectTitle = [NSString stringWithFormat:@"%ld",allNumber];
weakMoney.selectTitle = [[MethodTool shareTool]stringDisposeWithFloat:allmoney ];
}
}
};
也可以这样,这样不但书写方便,关键是大大增加可读性,并减少内存占用。
__weak typeof(self)weakSelf = self;
_numberTF.block = ^(NSInteger cellTag) {
[weakSelf TFNumber];
};
- (void)TFNumber
{
if (!SelectView[8].selectTitle) {
_numberTF.text = @"";
[MBProgressHUD showError:@"请先选择货号"];
}else{
if (_numberTF.text.length>0) {
NSInteger allNumber = self.fhsrate*[_numberTF.text integerValue];
CGFloat allmoney = [SelectView.selectTitle doubleValue]*allNumber;
SelectView1.selectTitle = [NSString stringWithFormat:@"%ld",allNumber];
SelectView2.selectTitle = [[MethodTool shareTool]stringDisposeWithFloat:allmoney ];
}
}
}
2.关键字const、static、extern、UIKIT_EXTERN区别和用法
const
这个单词翻译成中文是“常量”的意思。在程序中我们知道“常量”的值是不能变的,固定的。所以const关键字的作用就是:
(1)const用来修饰右边的基本变量或指针变量
(2)被修饰的变量只读,不能被修改
static
-
修饰局部变量
保证局部变量永远只初始化一次,在程序的运行过程中永远只有一份内存, 生命周期类似全局变量了,但是作用域不变。这句话怎么理解呢?还是以代码例子来讲解吧。随便建一个工程,在一个控制器类上监听控制器view的点击事件方法:
但是我们再看看局部变量i被关键字static修饰后的情况:
-
修饰全局变量
使全局变量的作用域仅限于当前文件内部,即当前文件内部才能访问该全局变量。iOS中在一个文件声明的全局变量,工程的其他文件也是能访问的,但是我又不想让其他文件访问,这时就可以用static修饰它了,比较典型的是使用GCD一次性函数创建的单例,全局变量基本上都会用static修饰。
下面是一个GCD一次函数创建的单例
修饰函数
static修饰函数时,被修饰的函数被称为静态函数,使得外部文件无法访问这个函数,仅本文件可以访问。这个在oc语言开发中几乎很少用,c语言倒是能看到一些影子,所以不详细探讨。
extern
这个单词翻译过来是“外面的、外部的”。顾名思义,它的作用是声明外部全局变量。这里需要特别注意extern只能声明,不能用于实现,而且定义和分配内存都在原来类中。
UIKIT_EXTERN
UIKIT_EXTERN,是经过处理的extern,其用法可以完全类比extern进行使用。
3.查看app应用启动时间分布
DYLD_PRINT_STATISTICS 1
4.iOS App间常用的五种通信方式
- 1、URL Scheme
- 2、Keychain
- 3、UIPasteboard
- 4、UIDocumentInteractionController
-
5、local socket
详细栗子
5. NSInteger和int 的区别
在32位系统下面,int、long、指针长度都是4,没有找到32位机器,也就没验证。
我这是在64位系统下验证的,也就说,
至少在64位系统下,int 是4位,long 是8位,指针也是8位。
所以说使用 NSInteger 更好,会根据32位系统还是64位系统来确定变量的长度,更好的利用系统所提供的最大位数。
6.千位加分隔符
NSNumberFormatter *moneyFormatter = [[NSNumberFormatter alloc] init];
moneyFormatter.positiveFormat = @"###,##0.00";
NSString *formatString = [moneyFormatter stringFromNumber:@(1234567.123)]
打印的最终结果是:
1,234,567.12
7.关于数据持久化
关于数据持久化可以用这样的图来方便记忆。 iOS中的文件的拷贝、移动、新建、删除、读取等都是 NSFileManager来实现的,而且这个类对象是单例。
8. 把JSon的txt文件转 NSDictionary
NSString *path = [[NSBundle mainBundle] pathForResource:@"lnglatInformation" ofType:@"txt"];
NSData *data = [NSData dataWithContentsOfFile:path];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
9. 协议接口Protocol中添加@property
@protocol Mydelegate <NSObject>
@property (strong, nonatomic) NSString *MydelegateProperty;
- (void)hah ;
@end
@interface FirstVC : UIViewController
<Mydelegate>
@end
@implementation FirstVC
@synthesize MydelegateProperty = _MydelegateProperty;
SMK *mk = [SMK new];
mk.myde = self;
self.MydelegateProperty = @"asd";
NSLog(@"YYYY:%@",self.MydelegateProperty);
NSLog(@"UUUU:%@",mk.myde.MydelegateProperty);
重要的是两步:
<Mydelegate>
@synthesize MydelegateProperty = _MydelegateProperty;
即可赋值了。
10. NSURL 的系统API调用
NSURL 根据 RFC 2396 的定义提供了只读属性用于获取 URL 每一部分的值:
* scheme,协议名,不带有连接内容的冒号,如“http”。
* user,解码后的用户名。
* password,解码后的 password。
* host,主机域名或 IP 地址。
* port ,端口号。
* path,authority(user,password,host,port)后的部分,如果 URL 对象是 file URL,该属性会去掉最后的/。
* query,解码后的 query 字符串,没有?,但包括连接多个键值对的&。
* fragment,解码后的片段,不包括#。
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com:88/path/cash?id=98&name=98"];
url.scheme : http
url.host : www.baidu.com
url.port : 88
url.path : /path/cash
url.query : id=98&name=98
具体的例子如:
hierarchical part
┌───────────────────┴─────────────────────┐
authority path
┌───────────────┴───────────────┐┌───┴────┐
abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1
└┬┘ └───────┬───────┘ └────┬────┘ └┬┘ └─────────┬─────────┘ └──┬──┘
scheme user information host port query fragment
urn:example:mammal:monotreme:echidna
└┬┘ └────────────┬───────────────┘
scheme path
URL中的非法字符串的编译
由于url支持26个英文字母、数字和少数几个特殊字符,因此,对于url中包含非标准url的字符时,
就需要对其进行编码。iOS中提供了函数stringByAddingPercentEscapesUsingEncoding对中文和一些特殊字符进行编码,
但是stringByAddingPercentEscapesUsingEncoding的功能并不完善,对一些较为特殊的字符无效。
而对这些字符则可以使用CFURLCreateStringByteAddingPercentEscapes函数,`
NSString *str = [NSString stringWithUTF8String:surl.c_str()];
str = @"http://218.21.213.10/MobileOA/TIFF/鄂安办发45号关于下达2012年全市安全生产相对控制指标的通知1.jpg";
str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];