IOS判斷NSArray是否為空

2022-09-08 05:06:13 字數 3599 閱讀 3630

場景描述:判斷乙個集合是否為空,如果不為空執行a,如果為空執行b

j**a實現方法

public void exec()else

}

ios如何實現?先看下下面**執行結果

- (void)viewdidload

-(void) show:(nsarray *)aryelse

if (ary) else

if (ary.count == 0) else

nslog(@"********************=華麗的分割線********************===");

}

執行結果如下:

2014-07-29 15:51:55.209 mytemptest[297:60b]陣列內容:(null)

2014-07-29 15:51:55.211 mytemptest[297:60b]陣列class(null)

2014-07-29 15:51:55.212 mytemptest[297:60b] not!!!   iskindofclass null

2014-07-29 15:51:55.213 mytemptest[297:60b] ====*****

2014-07-29 15:51:55.215 mytemptest[297:60b] count is 0

2014-07-29 15:51:55.216 mytemptest[297:60b] ********************=華麗的分割線********************===

2014-07-29 15:51:55.217 mytemptest[297:60b]陣列內容:(null)

2014-07-29 15:51:55.218 mytemptest[297:60b]陣列class(null)

2014-07-29 15:51:55.219 mytemptest[297:60b] not!!!   iskindofclass null

2014-07-29 15:51:55.220 mytemptest[297:60b] ====*****

2014-07-29 15:51:55.221 mytemptest[297:60b] count is 0

2014-07-29 15:51:55.222 mytemptest[297:60b] ********************=華麗的分割線********************===

2014-07-29 15:51:55.223 mytemptest[297:60b]陣列內容:(

)

2014-07-29 15:51:55.225 mytemptest[297:60b]陣列class__nsarrayi

2014-07-29 15:51:55.226 mytemptest[297:60b] not!!!   iskindofclass null

2014-07-29 15:51:55.227 mytemptest[297:60b] ====非空*****

2014-07-29 15:51:55.228 mytemptest[297:60b] count is 0

2014-07-29 15:51:55.229 mytemptest[297:60b] ********************=華麗的分割線********************===

2014-07-29 15:51:55.230 mytemptest[297:60b]陣列內容:(

11111,

222

)

2014-07-29 15:51:55.231 mytemptest[297:60b]陣列class__nsarraym

2014-07-29 15:51:55.232 mytemptest[297:60b] not!!!   iskindofclass null

2014-07-29 15:51:55.233 mytemptest[297:60b] ====非空*****

2014-07-29 15:51:55.234 mytemptest[297:60b] count is 2

2014-07-29 15:51:55.235 mytemptest[297:60b] ********************=華麗的分割線********************===

結論:ios判斷乙個陣列是否為空,直接判斷「ary.count」即可,即使ary尚未初始化也不會報「空指標」。

nsstring *str ;

nslog(@"%lu",(unsigned long)str.length);

2014-07-30 08:40:52.851 mytemptest[391:60b] 0

objective-c裡面宣告乙個型別,未初始化,直接呼叫方法都不報錯?

ios開發 判斷是否為空

1.字串判空 bool isblankstring nsstring string if string nil string null return yes if string iskindofclass nsnull class return yes if string stringbytrimm...

判斷是否為陣列

在說明如何判斷乙個物件為陣列型別前,我們先鞏固下js的資料型別,js一共有六大資料型別 number string object boolean null undefined。除了前四個型別外,null 物件 陣列返回的都是object型別 對於函式型別返回的則是function,再比如typeof...

判斷是否為質數

程式接受兩個正整數的輸入,構成乙個閉區間,找出這個區間內的所有質數。判斷乙個數n是否為質數時,先對這個數開平方,隨後從2開始,迴圈到這個平行根,檢查其中的數能否整除數n,若能整除,則為合數,否則為質數。由於sqrt函式的返回值是double型別,為保證準確性 如sqrt 4 返回值可能是1.9999...