日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS 高阶

發(fā)布時間:2024/7/19 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS 高阶 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.UIStoryBoard

2. segue跳轉傳值

3. UIColor配色

? ??

? ? //1. 十進制配色

? ? [UIColor colorWithRed:163.0/255.0 green:148.0/255.0 blue:128.0/255.0 alpha:1.0];

? ? //2. 設置16進制顏色(同上)

? ? [UIColor colorWithRed:0xba/255.0 green:0x21/255.0 blue:0xd5/255.0 alpha:1.0];

4. XML解析

?

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property(nonatomic,copy) NSString *pid;

@property(nonatomic,copy) NSString *name;

@property(nonatomic,copy) NSString *sex;

@property(nonatomic,copy) NSString *age;

@end

?

#import <Foundation/Foundation.h>

#import "Person.h"

@interface XMLUtil : NSObject<NSXMLParserDelegate>

@property(nonatomic,strong) NSXMLParser *par;

@property(nonatomic,strong) Person *person;

//存放每個person

@property(nonatomic,strong) NSMutableArray *list;

//標記當前標簽,以索引找到XML文件的內容

@property(nonatomic,copy) NSString *currentElement;

//聲明parse方法,通過它實現(xiàn)解析

-(void)parse;

@end

@implementation XMLUtil

?

-(instancetype)init {

?? ?

? ? self = [super init];

?? ?

? ? if (self) {

?? ? ? ?

? ? ? ? //獲取事先準備好的XML文件

? ? ? ? NSBundle *b = [NSBundle mainBundle];

? ? ? ? NSString *path = [b pathForResource:@"test" ofType:@".xml"];

?? ? ? ?

? ? ? ? NSData *data = [NSData dataWithContentsOfFile:path];

? ? ? ? self.par = [[NSXMLParser alloc] initWithData:data];

?? ? ? ?

? ? ? ? //添加代理

? ? ? ? self.par.delegate = self;

? ? ? ? //初始化數(shù)組,存放解析后的數(shù)據(jù)

? ? ? ? self.list = [NSMutableArray arrayWithCapacity:5];

?? ? ? ?

? ? }

?? ?

? ? return self;

?? ?

}

?

//幾個代理方法的實現(xiàn),是按邏輯上的順序排列的,但實際調用過程中中間三個可能因為循環(huán)等問題亂掉順序

//(1).開始解析

-(void)parseDiaStartCocument:(NSXMLParser *)parser {

?? ?

? ? NSLog(@"parserDidStartDocument...");

}

?

//(2).準備節(jié)點

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict {

?? ?

? ? self.currentElement = elementName;

? ? if ([self.currentElement isEqualToString:@"student"]) {

?? ? ? ?

? ? ? ? self.person = [[Person alloc] init];

?? ? ? ?

? ? }

}

?

//(3). 獲取節(jié)點內容

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

?? ?

? ? if ([self.currentElement isEqualToString:@"pid"]) {

?? ? ? ?

? ? ? ? [self.person setPid:string];

? ? }else if ([self.currentElement isEqualToString:@"name"]) {

? ? ?

? ? ? ? [self.person setName:string];

? ? }else if ([self.currentElement isEqualToString:@"sex"]) {

?? ? ? ?

? ? ? ? [self.person setSex:string];

? ? }else if ([self.currentElement isEqualToString:@"age"]) {

?? ? ? ?

? ? ? ? [self.person setAge:string];

? ? }

}

?

//(4). 解析完一個節(jié)點

-(void)parseDidEndDocument:(NSXMLParser *)parser {

?? ?

? ? NSLog(@"parserDidEndDocument...");

}

?

//外部調用借口

-(void)parse {

?? ?

? ? [self.par parse];

}

@end

5. JSON解析

?//1. Json數(shù)據(jù)封包

? ? NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value1",@"key1",@"value2",@"key2",@"value3",@"key3", nil];

?? ?

? ? //isValidJSONSerialization判斷對象是否可以構建成json對象

? ? if ([NSJSONSerialization isValidJSONObject:dic]) {

?? ? ? ?

? ? ? ? NSError *error;

? ? ? ? //創(chuàng)建一個json從Data,NSJSONWritingPrettyPrinted指定的JSON數(shù)據(jù)產的空白,使輸出更具可讀性

? ? ? ? NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];

? ? ? ? NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

?? ? ? ?

? ? ? ? NSLog(@"json data:%@",json);

? ? }

? ? //2. Json數(shù)據(jù)解析

? ? NSError *error;

? ? //加載一個NSURL對象

? ? NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.weather.com.cn/data/101120101.html"]];

? ? //將請求的url數(shù)據(jù)放到NSData對象中

? ? NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

? ? //IOS自帶解析類NSJSONSerialization從response中解析出數(shù)據(jù)放到字典中 - crash here - TBD

? ? NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];

?? ?

? ? NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"];

?? ?

//? ? NSDictionary *text = [NSString stringWithFormat:@"今天是%@ %@ %@ 的天氣狀況是:%@ %@",

//? ? ? ? ? ? ? ? ? ? ? ? ? [weatherInfo objectForKey:@"day_y"],

//? ? ? ? ? ? ? ? ? ? ? ? ? [weatherInfo objectForKey:@"week"],

//? ? ? ? ? ? ? ? ? ? ? ? ? [weatherInfo objectForKey:@"city"],

//? ? ? ? ? ? ? ? ? ? ? ? ? [weatherInfo objectForKey:@"weather1"],

//? ? ? ? ? ? ? ? ? ? ? ? ? [weatherInfo objectForKey:@"temp1"]];

? ? NSDictionary *text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天氣狀況是: %@ %@",[weatherInfo objectForKey:@"day_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"],[weatherInfo objectForKey:@"weather1"],[weatherInfo objectForKey:@"temp1"]

? ? ? ? ? ? ? ? ? ? ? ? ? ];

? ? NSLog(@"weatherInfo: %@",text);

6. 多線程

? ? 1.NSThread - 蘋果封裝后,并且完全面向對象

//? ? //(1). 先創(chuàng)建線程類,在啟動

//? ? //創(chuàng)建

//? ? NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:nil];

// ? ?

//? ? //啟動

//? ? [thread start];

// ? ?

//? ? //(2). 創(chuàng)建并啟動

//? ? [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:nil];

?? ?

?? ?

? ? //2. GCD - 蘋果為多核的并行運算提出的解決方案

?? ?

? ? //在GCD中,加入了2個非常重要的概念:任務和隊列.

? ? //隊列:分串行和并行2種

? ? //(1). 主隊列:用于刷新UI

? ? //創(chuàng)建

//? ? dispatch_queue_t queue = dispatch_get_main_queue();

?? ?

? ? //(2).自己創(chuàng)建的隊列:用于耗時漸咋

? ? //串行隊列

//? ? dispatch_queue_t queue = dispatch_queue_create("tk.bourne.testQueue", NULL);

? ? //并行隊列

//? ? dispatch_queue_t queue = dispatch_queue_create("tk.courne.testQueue", DISPATCH_QUEUE_SERIAL);

?? ?

? ? //(3). 全局并行隊列:只要是并行隊列任務一般都加入到這個隊列,這是系統(tǒng)提供的一個并發(fā)隊列

? ? dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_SERIAL, 0);

?? ?

? ? //任務:分同步和異步兩種

? ? //(1).同步任務:不會另開線程,會阻塞當前線程

? ? dispatch_sync(queue, ^{

? ? ? ? ? ? ? ? ? //code here

? ? });

?? ?

?? ?

? ? //(2).異步任務:會另開線程,不會阻塞當前線程

? ? dispatch_sync(queue, ^{

? ? ? ? //code here

? ? });

?? ?

?? ?

? ? //3. NSOperation和NSOperationQueue

? ? /*

? ? ? ? 對GCD的封裝,完全面向對象,NSOperation和NSOperationQueue分別對應GCD的任務和隊列。

? ? ? ? 操作步驟:? 1. 將要執(zhí)行的任務封裝到一個NSOperation對象中.

?? ? ? ? ? ? ? ? 2. 將此任務添加到一個NSOperationQueue對象中.

?? ?

? ? ? ? 注意:NSOperation只是一個抽象類,所以不能封裝任務。但它有2個子類用于封裝任務。

? ? ? ? ? ? 分別是:NSInvocationOperation 和 NSBlockOperation。創(chuàng)建一個Operation后,

? ? ? ? ? ? 需要調用start方法來啟動任務,它會默認在當前隊列同步執(zhí)行。當然可以在中途取消一個任務,

? ? ? ? ? ? 只需要調用起cancel方法即可.

?? ? */

?? ?

? ? //NSInvocationOperation:需要傳入一個方法名

? ? //1.創(chuàng)建NSInvocation對象

//? ? NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run) object:nil];

// ? ?

//? ? //2.開始執(zhí)行

//? ? [operation start];

?? ?

? ? //1.創(chuàng)建NSBlockOperation對象

? ? NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{

?? ? ? ?

?? ? ? ?

?? ? ? ?

? ? }];

?? ?

? ? //可以添加多個block

? ? for (NSInteger i = 0; i < 5; i ++ ) {

? ? ? ? [operation addExecutionBlock:^{

?? ? ? ? ? ?

? ? ? ? ? ? NSLog(@"第%ld次: %@",i,[NSThread currentThread]);

? ? ? ? }];

? ? }

?? ?

? ? //2.開始執(zhí)行

? ? [operation start];

7.動畫效果

8. NSUserDefaults

//.簡介

? ? /*

?? ? ? ? NSUserDefaults是一個單例

? ? ? 他可以用于數(shù)據(jù)的永久保存,而且簡單實用,這是它可以讓數(shù)據(jù)自由傳遞的一個前提,

? ? ? 也是大家喜歡用它保存簡單數(shù)據(jù)的一個主要原因.

?? ? */

?? ?

?? ?

? ? //1. NSUserDefaults

? ? // NSUserDefaults支持的數(shù)據(jù)類型有: NSNumber(NSInteger、float、double),NSString、NSDate、NSArray、NSDictionary、BOOL;

? ? //將NSString對象存儲到NSUserDefaults中

//? ? NSString *passWord = @"123456";

//? ? NSUserDefaults *user = [NSUserDefaults standardUserDefaults];

//? ? [user setObject:passWord forKey:@"userPassWord"];

?? ?

? ? //將數(shù)據(jù)取出來

? ? NSUserDefaults *user = [NSUserDefaults standardUserDefaults];

? ? NSString *passWord = [user objectForKey:@"userPassWord"];

?? ?

?? ?

?? ?

? ? //2.NSUserDefaults存儲對象

? ? //NSUserDafaults本身不支持自定義對象的存儲,不過它支持NSData的類型,所以可以選擇使用歸檔,將自定義對象轉為NSData類型后存入NSUserDefaults.

?? ?

? ? //(1).將Student類的數(shù)據(jù)歸檔

? ? //(2).將Student對象存入NSUserDefaults

? ? Student *student = [[Student alloc] int];

? ? student.name = @"ladyO";

? ? student.sex = @"女";

?? ?

? ? NSData *data = [NSKeyedArchiver archivedDataWithRootObject:student];

? ? NSUserDefaults *user = [NSUserDefaults standardUserDefaults];

? ? [user setObject:data forKey:@"oneStudent"];

? ? //(3).從NSUserDefaults取出Student對象

? ? NSUserDefaults *user = [NSUserDefaults standardUserDefaults];

? ? NSData *data = [user objectForKey:@"oncStudent"];

? ? Student *student = [NSKeyedArchiver unarchiveObjectWithData:data];

9. 網(wǎng)絡請求

?

轉載于:https://www.cnblogs.com/share-iOS/p/6131137.html

總結

以上是生活随笔為你收集整理的iOS 高阶的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。