[MISS靜IOS開發原創文摘]-AppDelegate儲存全域變數和 NSUserDefaults standardUserDefaults 通過模型儲存和讀取資料,儲存自訂的對象

來源:互聯網
上載者:User

標籤:

由於app開發的需求,需要從api介面獲得json格式資料並儲存臨時的 app的佈景主題色彩 和 相關url

 

方案有很多種:

1, 通過AppDelegate儲存為全域變數,再擷取

2,使用NSUSerDefault

 

第一種 :通過AppDelegate方法:

 

定義全域變數

////  AppDelegate.h////  Created by MISSAJJ on 15/5/5.//  Copyright (c) 2015年 MISSAJJ. All rights reserved.//#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate >@property (strong, nonatomic) UIWindow *window;@property (strong, nonatomic) NSString *globalAppThemeColor;@property (strong, nonatomic) NSString *globalAboutTag; @end

 

在AppDelegate.m 內賦值:

_globalAppThemeColor = appDetails.appThemeColor;_globalAboutTag = appDetails.about;

 

在需要的VC頭部匯入

#import "AppDelegate.h"

 

- (void)viewDidLoad {    [super viewDidLoad];       //建立   AppDelegate * appDelegate=(AppDelegate*)[[UIApplication sharedApplication]delegate]; }  

 

獲得變數

NSString *about = appDelegate.globalAboutTag;
NSString *theme = appDelegate.globalAppThemeColor;

靈活運用到代碼需求的地方

//navi設定為全域主題色self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:appDelegate.globalAppThemeColor alpha:1];

 

 ====== I am 華麗麗的分割線 ^_^=======

 

 

第二種 :通過NSUserDefaults方法:

尋找了相關資料,自己整理了一份NSUserDefaults文摘給有共同有需求的程式猿朋友們.

一  ,NSUserDefaults 簡單的運用方法

NSUserDefaults一般可以存取一些短小的資訊,比如存入再讀出一個字串到NSUserDefaults

 

注意 : key值必須要相同才能讀取出來哦!

 NSUserDefaults只支援: NSString, NSNumber, NSDate, NSArray, NSDictionary,  不是所有資料都能往裡放滴哦~

//儲存資料NSString *string = [NSString stringWithString @"我想儲存的字串內容"];     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];     [userDefaults setObject:string forKey:@"theme"]; 
//在儲存資料的地方,別忘了這一句 [[NSUserDefaults standardUserDefaults] synchronize];

//在需要的地方擷取資料NSString *getStringValue = [[NSUserDefaults standardUserDefaults] objectForKey:@"theme"];


 


二 , 如果需要儲存比較多得資料, 可以通過模型儲存和讀取

 

1.模型代碼

 

////  AreasModel.h//////  Created by MISSAJJ on 15/5/7.//  Copyright (c) 2015年 MISSAJJ. All rights reserved.//#import <Foundation/Foundation.h>@interface AreasModel : NSObject /* * about 模型 */@property (nonatomic,copy)NSString * about;@property (nonatomic,copy)NSString * appThemeColor; @end

 

記住必須要在 M 檔案 裡 寫這兩個方法

- (id) initWithCoder: (NSCoder *)coder

- (void) encodeWithCoder: (NSCoder *)coder

然後把該自訂的類對象編碼到 NSData中,再從NSUserDefaults中進行讀取。

 

////  AreasModel.m//////  Created by MISSAJJ on 15/5/7.//  Copyright (c) 2015年 MISSAJJ. All rights reserved.//#import "AreasModel.h"@implementation AreasModel- (id)initWithCoder:(NSCoder *)aDecoder{    if(self = [super init])    {        self.about       = [aDecoder decodeObjectForKey:@"about"];        self.appThemeColor           = [aDecoder decodeObjectForKey:@"appThemeColor"];            }    return self;}- (void)encodeWithCoder:(NSCoder *)aCoder{    [aCoder encodeObject:self.about forKey:@"about"];    [aCoder encodeObject:self.appThemeColor forKey:@"appThemeColor"];  }@end

 

 

2. 儲存資料的代碼

 

//////////////////////////   
以上省略........
//////////////////////////

//URL編碼成UTF8 dirPath = [dirPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL * dirUrl = [NSURL URLWithString:dirPath]; NSMutableURLRequest * dirRequest = [NSMutableURLRequest requestWithURL:dirUrl]; NSData *dirJsonData = [NSURLConnection sendSynchronousRequest:dirRequest returningResponse:nil error:nil]; NSDictionary *dirListJsonData = [NSJSONSerialization JSONObjectWithData:dirJsonData options:0 error:nil]; NSDictionary* dicData = [dirListJsonData objectForKey:@"data"]; #pragma mark ====儲存臨時主題和關於資訊==== NSString *about = [dicData objectForKey:@"about"]; NSString *theme = [dicData objectForKey:@"theme"]; //建立模型 AreasModel *themeAndAbout = [[AreasModel alloc] init]; themeAndAbout.about = about; themeAndAbout.appThemeColor = theme; //儲存資料,用歸檔儲存到NSUserDefault NSData *themeAndAboutData = [NSKeyedArchiver archivedDataWithRootObject:themeAndAbout]; [[NSUserDefaults standardUserDefaults] setObject:themeAndAboutData forKey:@"themeAndAbout"]; [[NSUserDefaults standardUserDefaults] synchronize];

 

3. 擷取資料代碼 

 

//獲得儲存資料    NSData *getthemeAndAboutData = [[NSUserDefaults standardUserDefaults] objectForKey:@"themeAndAbout"]; 

//轉成模型擷取資料 AreasModel *getThemeAndAbout = [NSKeyedUnarchiver unarchiveObjectWithData:getthemeAndAboutData]; NSLog(@"%@,%@",getThemeAndAbout.appThemeColor, getThemeAndAbout.about);

 

 

 ====心靈和身體,總有一個在路上=== ====只要還在路上,就不至於後退===     

[MISS靜IOS開發原創文摘]-AppDelegate儲存全域變數和 NSUserDefaults standardUserDefaults 通過模型儲存和讀取資料,儲存自訂的對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.