ios 常用的一些方法和注意点 componentsSeparatedByString componentsJoinedByString以及NSURl和NSURLComponents

将string字符串转换为array数组

 NSArray  *array = [Str componentsSeparatedByString:@","];

注意://componentsSeparatedByString 这个方法有一个bug 当被切割的字符串是 @“” 时  切割后  返回的 数组长度为1 元素为 @“”

反向方法

将array数组转换为string字符串

 NSString *tempString = [mutableArray componentsJoinedByString:@","];--分隔符

在图片加载过大而导致性能卡顿延时,解决办法?

1.压缩为jpeg格式,jpeg比png的要小很多

2.可以选择等比压缩图片大小,这个可以选择和服务器沟通,例如:https://www.baidu.com/resize_200x200/group1/M00/7F/CB/wKgURFutllqADQJuAAMjFhc7sF8559.png

服务器根据resize_200x200来通知客户端所需要处理的图片大小,来设置image的大小

NSUrl有的参数  

@property (nullable, readonly, copy) NSString *scheme;

@property (nullable, readonly, copy) NSString *resourceSpecifier;

@property (nullable, readonly, copy) NSString *host;

@property (nullable, readonly, copy) NSNumber *port;

@property (nullable, readonly, copy) NSString *user;

@property (nullable, readonly, copy) NSString *password;

@property (nullable, readonly, copy) NSString *path;

@property (nullable, readonly, copy) NSString *fragment;

@property (nullable, readonly, copy) NSString *parameterString;

@property (nullable, readonly, copy) NSString *query;

@property (nullable, readonly, copy) NSString *relativePath; 

 

NSURLComponents

该类苹果在 iOS 7中添加,它(NSURLComponents)可以方便的把 URL 地址分解成多个部分;

其中, URL(Uniform Resource Locator)地址用于描述一个网络上的资源,基本格式如下:

schema://host[:port#]/path/.../[?query-string][#anchor]

schema:指定低层使用的协议,例如 http https ftp 等

host:HTTP 服务器的 IP 地址或者域名

port#:HTTP 服务器的默认端口是80,该情况下端口号可以省略.如果使用其它端口,必须将其指明(http://www.cnblogs.com:8080/)

path:访问资源的路径

query-string:发送给 http 服务器的数据

anchor: 锚

例如:

    if (!name || [name isEqualToString:@""]) {

        return nil;

    }

    NSURLComponents *components = [NSURLComponents componentsWithURL:self resolvingAgainstBaseURL:YES];

    if (!components.queryItems) return nil;

    

    for (NSURLQueryItem *item in components.queryItems) {

        if ([item.name isEqualToString:name]) {

            return item.value;

        }

    }

这些都非常好用的,其实苹果好多接口我们都很少用到,多接触也就好了

猜你喜欢

转载自blog.csdn.net/georgehenrywilliam/article/details/82888911