• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ObjectiveC基础教程笔记

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

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

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

#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);


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Objective-C精选字符串处理方法发布时间:2022-07-12
下一篇:
【iOS开发之Objective-C】书签管理器项目发布时间:2022-07-12
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap