iOS Developer

Objective-C 基础学习笔记

2017-01-12  本文已影响20人  RadioHeadache

语法问题

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
上一篇 下一篇

猜你喜欢

热点阅读