Objective-C 基礎學習筆記

語法問題

dot syntax

BNRitem *item = [[BNRItem alloc]init];
[item setValueInDollars:5]
int value = [item valueInDollars];

//也可以寫成,這種方法叫做dot syntax
BNRitem *item = [[BNRItem alloc]init];
item.valueInDollars = 5;//這里點后面的名字變成了getter方法的名字,
但是還是setter方法
int value = item.valueInDollars;

overriding method 方法的覆蓋

子類繼承父類的方法,可以直接調用,但是也可以通過在@implementation里面進行重寫方法,再次調用時,就能覆蓋方法。

description的作用

如果在main.m里面輸入

NSLong(@"%@", item);

就會直接輸出類的名字以及item的值,也就是對象的地址


What is designated initializer

For each class, regardless of how many initialization methods there are, one method is chosen as the ****designated initializer****.

The ****designated initializer**** makes sure that every instance variable of an object is valid.


declare部分的方法順序

  1. Instance Variables
  2. Class Method
  3. Initializers
  4. Other Instance Methods

Array的相關問題

創建Array

NSArray *randomAdjectiveList = @[@"Fluffy", @"Rusty", @"shiny"];
    
NSArray *randomNounList = @[@"Bear", @"Spork", @"Mac"];

以@符號開頭,后面接著方括號,括號里用逗號隔開

Enumerating Arrays 數組的枚舉

NSArray *germanMakes = @[@"Mercedes-Benz", @"BMW", @"Porsche",
                         @"Opel", @"Volkswagen", @"Audi"];
// With fast-enumeration
for (NSString *item in germanMakes) {
    NSLog(@"%@", item);
}
// With a traditional for loop
for (int i=0; i<[germanMakes count]; i++) {
    NSLog(@"%d: %@", i, germanMakes[i]);
}

其中第一個叫做快速枚舉,第二個是傳統方法枚舉


遍歷數組

把數組里面的元素從頭到尾訪問一遍,然后輸出

for (int i = 0; i < 10; i++){
            BNRItem *item = [BNRItem randomItem];
            [items addObject:item];
        }
        
        
//fast enumeration
        for (BNRItem *item in items) {
            NSLog(@"%@", item);
        }


Adding and removing object

The two basic methods for manipulating the contents of an array are the addObject: and removeLastObject methods. The former adds an object to the end of the array, and the latter is pretty self-documenting. Note that these are also useful methods for treating an NSArray as a stack.

NSMutableArray *brokenCars = [NSMutableArray arrayWithObjects:@"Audi A6", @"BMW Z3",@"Audi Quattro", @"Audi TT", nil];

[brokenCars addObject:@"BMW F25"];
NSLog(@"%@", brokenCars);       
// BMW F25 added to end
[brokenCars removeLastObject];
NSLog(@"%@", brokenCars);       
// BMW F25 removed from end

It’s most efficient to add or remove items at the end of an array, but you can also insert or delete objects at arbitrary locations using ****insertObject:atIndex:**** and ****removeObjectAtIndex:****. Or, if you don’t know the index of a particular object, you can use the removeObject: method, which is really just a convenience method for indexOfObject: followed by removeObjectAtIndex:.

// Add BMW F25 to front
[brokenCars insertObject:@"BMW F25" atIndex:0];
// Remove BMW F25 from front
[brokenCars removeObjectAtIndex:0];
// Remove Audi Quattro
[brokenCars removeObject:@"Audi Quattro"];

It’s also possible to replace the contents of an index with the ****replaceObjectAtIndex:withObject:**** method, as shown below.

//change second item 
[brokenCars replaceObjectAtIndex:1 withObject:@"Audi Q5"];

其他

ObjectAtIndex

輸入對象在數組里面的排序,返回對象

NSString * foo = [items objectAtIndex:0]
//等于
NSString * foo = items[0]//方括號里是序號

shorthand for ObjectAtIndex

[randomAdjectiveList ObjectAtIndex:adjectiveIndex]

randomAdjectiveList [adjectiveIndex]

count

count是對象方法,返回數組里面元素的個數

NSMutableArray

ShortHand

NSMutableArray *items = [[NSMutableArray alloc]init];
items[0] = @"a";//add "a"
items[1] = @"b";//add "b"
items[0] = @"c";//replace "a" with "c"

They are equal with...

NSMutableArray *items = [[NSMutableArray alloc]init];
[items insertObject:@"a" atIndex:0];
[items insertObject:@"b" atIndex:1];
[items replaceObjectAtIndex:0 withObject:@"c"];

隨機數的產生

產生的是從0到x的隨機數,[0 , X)

int value = arc4random() % x; 

如果要產生[a , b]的隨機數,就要

//假設c = b - a
int value = a + arc4random() % c + 1
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容