Objective C基础教程笔记

只要多一个间接层,就没有解决不了的问题

不在代码中直接使用某个值,而是使用指向该值的指针

基本变量就是间接的一种实际应用

#import <Foundation/Foundation.h>

int main (int argc, const char *argv[] )

{

  NSLog(@"The numbers from 1 to 5:");

  int i;

  for(i=1;i<=5;i++) {

    NSLog(@"%d\n", i);  

  }

  return (0);

}//main

 

文件是另一种间接的示例

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) 

{

  const char *words[4] = {"aardvark","abacus","allude","zygote"};

  int wordCount = 4

  int i;

  for(i=0;i<wordCount;i++) {

    NSLog(@"%s is %d characters long",words[i], strlen(words[i]));

  }

  return (0);

}

 

#import<Foundation/Foundation.h>

int main(int argc, const char * argv[])

{

  const char *words[4] = {"Joe-Bob \"Handyman\" Brown","Jacksonville \"Sly\" Murphy","Shinara Bain","George \"Guitar\" Books"};

  int wordCount = 4;

  int i;

  for(i=0;i<wordCount;i++) {

    NSLog(@"%s is %d characters long", words[i],strlen(words[i]));

  }

  return (0);

}

 

#import <Foundation/Foundation.h>

int main(int argc, const char *argv[])

{

  FILE *wordFile = fopen("/tmp/words.txt","r");

  char word[100];

  while(fgets(word,100,wordFile)) {

    word(strlen())

  }

}

 

#import <MapKit/Mapkit.h>

How Gigantic Are Frameworks?

Inheritance

simply means that a newly defined class automatically gets all of the chacteristics and behaviors of an existiong claa, and add one or more items that make the new  class something different.

the most basic object in the DOM structure is the node.

noeName, nodeValue, nodeType,firstChild appendChild, removeChild cloneNode

Element   getAttribute()   setAStrtribute   getElemntsByTagName()

HTMLElement  id title, className 

HTMLImage   loading and displaying images   It is only by virtue of the inheritance chain 

 

Integrating the DGCar Class into Workbench

WorkbenchViewController.m file  runMyCode   

Add an import directive to the WorkbenchViewConroller 

#import "DGCar.h"

- (IBAction)runMyCode:(id)sender{

  DGCar *myCar1 = [[DGCar alloc] initWithCarMake:@"Chevrolet"

                         model:@"Malibu" year:@"2007"];

  NSLog(@"I used to drive a:%@",[myCar1 getFormattedListing]);

}

 

needs some memory management

associate this method with a button in Interface Builder , we specified the return type as IBAction

IBAction is the same as void ,to be ready to connect with Inerface Builder and offers the methods as a choice when it commes time to connect the eent to the code 

- (IBAction)methodName

- (IBAction)methodName:(id)sender

-(IBAction)methodName:(id)sender (UIEvent *)event

UIEvent object is an analogous to the event object that is passed to Javascripot/DOM eent handler functions 

 

sender is a reference to the button that the user clicks,and i chose this format for demonstration purposes.

Creating Object Instances

DGCar *myCar1 = [[DGCar alloc] initWithCarMake:@"Chevrolet"

                       model:@"Malibu" year:@"2007"];

sends two messages nested inside each other

 

The alloc method belongs to the NSObject superclass to reserve a chunk of memory for use by the object instance.

recovering that memory when your're done with the object.

 

NSLog() and String Formats

NSLog(@"Button was pressed.");

begin with the  @ 

string format placeholders

% the type of the data to sent to the console . if the value is an boject ,the spcifier combination is %@

Values to be plugged into the specifier appear after the quoted string . separated by a comma

NSLog(@"I userd to drive a:%@",[myCar1 getFormattedListing]);

description method,which returns a meaning full string representing the object or ites data.

NSArray 

%d, %f

NSString *myCar1Description = [myCar1 getFormattedListing];

NSLOG(@"My Cars:\nThen - %@\nNow -%@", myCar1Description, myCar2Description);

posted @ 2012-07-27 08:35  顺武  阅读(225)  评论(0编辑  收藏  举报