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 方法的覆蓋

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

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的相關問題

創(chuàng)建Array

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

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

Enumerating Arrays 數(shù)組的枚舉

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]);
}

其中第一個叫做快速枚舉,第二個是傳統(tǒng)方法枚舉


遍歷數(shù)組

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

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

輸入對象在數(shù)組里面的排序,返回對象

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

shorthand for ObjectAtIndex

[randomAdjectiveList ObjectAtIndex:adjectiveIndex]

randomAdjectiveList [adjectiveIndex]

count

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

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"];

隨機數(shù)的產(chǎn)生

產(chǎn)生的是從0到x的隨機數(shù),[0 , X)

int value = arc4random() % x; 

如果要產(chǎn)生[a , b]的隨機數(shù),就要

//假設c = b - a
int value = a + arc4random() % c + 1
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 這里僅記錄對我有幫助的內(nèi)容,不是對這本書的全面概括。有些我目前還沒怎么用過的東西,比如GCD,現(xiàn)在讀來還沒什么感覺...
    倫啊倫閱讀 350評論 0 1
  • 這篇文章是自己學習Swift的筆記與深化。希望這篇文章能夠幫助已經(jīng)有Objective-C經(jīng)驗的開發(fā)者更快地學習S...
    王小賓閱讀 4,303評論 5 70
  • 這篇文章是學習Swift的筆記與深化。希望這篇文章能夠幫助已經(jīng)有Objective-C經(jīng)驗的開發(fā)者更快地學習Swi...
    YangPu閱讀 1,824評論 2 27
  • # Swift學習: 從Objective-C到Swift 這篇文章是自己學習Swift的筆記與深化。希望這篇文章...
    蘋果上的小豌豆閱讀 1,180評論 0 5
  • 前言 Swift 貢獻給社區(qū) 作者 關于中文翻譯 條件語句 尤達表達式 nil 和 BOOL 檢查 黃金大道 復雜...
    一條魚的星辰大海閱讀 449評論 0 1

友情鏈接更多精彩內(nèi)容