IOS開發基礎知識--片段1

來源:互聯網
上載者:User

標籤:io   ar   os   使用   sp   for   strong   檔案   資料   

一:NSString與NSInteger的互換

NSInteger轉化NSString類型:[NSString stringWithFormat: @"%d", NSInteger];

NSString轉化 NSInteger類型:NSInteger = [NSString intValue]; 

*其它幾個同理   [NSString boolValue]、[NSString floatValue]、[NSString doubleValue]

 

二:Objective-c中集合裡面不能存放基礎類型,比如int string float等,只能把它們轉化成對象才可以存放,就是類NSNumber

a. NSNumber的用法如下

把基本類型封裝成對象的便利建構函式

-(id) initWithChar : (char) value;

-(id) initWithInt : (int) value;

-(id) initWithFloat : (float) value;

-(id) initWithBool: (BOOL) value;

把基礎資料型別 (Elementary Data Type)封裝成對象的便利構造器

+(id) numberWithChar : (char) value;

+(id) numberWithInt : (int) value;

+(id) numberWithFloat : (float) value;

+(id) numberWithBool : (BOOL) value;

從NSNumber中擷取值

-(char) charValue;    -(int) intValue;      -(float) floatValue;     (BOOL) boolValue;    (NSString *) stringValue;

 

b、在集合類中是不可以儲存nil(空的),因為nil作為nil的結束符,那麼我們如何來儲存Null 物件呢?該NSNull出場啦,其功能是把null封裝成對象,

+(NSNull *) null;

 

三:NSDATA與NSString互轉

NSData,資料,當我們需要把一些資訊寫入到檔案裡或發送到網路上,我們需要把這些資料轉換下,變成純粹的0、1字元流

NSString * str = @"hello, world!";

 NSData * data = [str dataUsingEncoding:NSUTF8StringEncoding]; //NSString轉換成NSData類型

 NSLog(@"%s", data.bytes);

 NSString * newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

 NSLog(@"%@", newStr);

 

四:去除輸入框空格(NSString也適用)

去除兩端空格

NSString *temp = [textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

去除兩端空格和斷行符號

NSString *text = [temp stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet ]];

 

五:IBOutlet,IBAction說明

執行個體:

@property(weak,nonatomic) IBOutlet UITextField *textFile1;

@property(weak,nonatomic) IBOutlet UILabel *lable1; -(IBAction)save:(id)sender;

IBOutlet 輸出口是使用關鍵字IBOutlet聲明的執行個體變數。控制器標頭檔中的輸出口聲明應如下所示: 在執行個體變數聲明前添加了IBOutlet語句,這表示對象在Interface Builder中建立的。一旦將對象連結到一個出口, 就可以像訪問任何自己建立的對象一樣訪問它。就編譯器而言,IBOutlet並未執行任何操作。它的唯一作用是告訴Interface Builder,此執行個體變數將被串連到nib中的對象。你建立的任何需要串連到nib檔案中的對象 的執行個體變數都必須以IBOutlet關鍵字開頭。開啟Interface Builder時,它會在項目標頭檔中掃描此關鍵字,你可以根據這些(且只能根據這些)變數將代碼串連到nib。

IBOutlet關鍵字的定義如下所示:

#ifndef IBOutlet

#define IBOutlet

#endif

IBAction 操作是控制器類中的方法。它們也是通過特殊關鍵字IBAction聲明的,該關鍵字告訴Interface Builder,此方法是一個操作,且可以被某個控制項觸發。

IOS開發基礎知識--片段1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.