8月笔记

本文介绍了Objective-C的基础知识,包括字符串操作、自定义类的创建、数组使用、类别扩展及协议实现等核心概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  
NSString * helloString = @"hello";

printf("%s\n", [helloString cString]);



2011-8-12
#import <Foundation/Foundation.h>

@interface Stu : NSObject
{
int age;
// NSString name;
}

- (int)Setage : (int)i;
//- (void)SetName(
- (void)Print;
@end

@implementation Stu

- (int)Setage : (int)i
{
age = i;
}

-(void)Print
{
NSLog(@"the age is %d", age);
}
@end

int main (int argc, const char * argv[]) {
// NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

// insert code here...
for (int i = 0; i < 3; i++)
{
NSLog(@"Hello, World! ^_^ %d ", i);
}

NSString * nstr = @"test";
NSLog(@"Hello, World! ^_^ ");

NSLog(@"** %d", boolTest(1, 2));
NSLog(@"YES=%d, NO=%d", YES, NO);

// NSLog(nstr);
NSLog(@"len=%d", strlen("friday"));

NSLog(@"--- %d", sum(1, 2));

Stu * pstu = [[Stu alloc] init];
[pstu Setage:22];
[pstu Print];

// [pool drain];
return 0;
}

省略标签

-(void)SetCode : (int)a :(int)b
{
age = a + b;
}

[pstu SetCode :10 :5];

--
-(void)SetCode : (int)a other:(int)b

[pstu SetCode :10 other:5];



--
2011-8-16
自定义的头文件在使用的时候,必须使用"" 而不是<>

对于一个类,不能直接 Stu p2; 进行定义,但结构体是可以的

--
2011-8-17
对象方法,类似于c++中的静态方法

+(void)Show;

+(void)Show
{
NSLog(@"show");
}

[Stu Show];


--
NSArray 数组的使用

NSArray * arr;
arr = [NSArray arrayWithObjects: @"one", @"two", @"three", nil];
for(int i = 0; i < [arr count]; i++)
{
NSLog(@"index %d is %@", i, [arr objectAtIndex:i]);
}


--
8-23
类别的使用
通过类别,可以添加方法

#import <Foundation/Foundation.h>


@interface NSString (testlen)

-(unsigned int) tLen;

@end


#import "Len.h"


@implementation NSString (testlen)

-(unsigned int) tLen
{
return 10 + [self length];
}

@end

//调用
NSLog(@"the length is %d", [@"8-23" tLen]);



选择器的使用,
判断类里是否有指定的方法

if ([pstu respondsToSelector:@selector(Print )]) {
NSLog(@"select");
}

if ([pstu respondsToSelector:@selector(Print :)]) {
NSLog(@"select2");
}

if ([pstu respondsToSelector:@selector(SetCode : : )]) {
NSLog(@"select3");
}

注意,方法有几个参数就应该有几个: ,没有参数就没有: ,多加: 不会在语法上报错,因为可能是重载的函数

--
一个协议使用的例子

#import <Foundation/Foundation.h>

//
@protocol Calculating
- (float) CalculateArea;
@optional
- (void) showing;
@required
- (float) CalculateLens;
@end;

//

@interface Rectangle : NSObject <Calculating> {
float length;
float width;
}
@property float length;
@property float width;

- (id) initWithLength: (float) length
Width: (float) width;
-(void) showRectangle;

@end

//
@implementation Rectangle
@synthesize length, width;

- (float) CalculateLens
{
float lensResult = length * 2 + width * 2;
return lensResult;
}

- (void) showing
{
NSLog(@"it is just showing!");
}

- (float) CalculateArea
{
float areaResult = length * width;
return areaResult;
}

- (id) initWithLength: (float) newLength
Width: (float) newWidth
{
if(self = [super init])
{
length = newLength;
width = newWidth;
}
return self;
}
-(void) showRectangle
{
NSLog(@"the Rectangle length is: %0.2f, width is: %0.2f", length, width);
}
@end
////////////

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

// insert code here...
NSLog(@"Hello, World!");

Rectangle * rectangle = [[Rectangle alloc] initWithLength: 8 Width: 6];
float resultArea = [rectangle CalculateArea];
[rectangle showRectangle];

[rectangle CalculateLens];

NSLog(@"the rectanle area is : %0.2f", resultArea);

[pool drain];
return 0;
}


--
8-24
使用gcc编译.m 文件
gcc -framework Foundation first.m -o first


--
8-25

系统共享路径
/Users/vica/Desktop/VMware Shared Folders/E

可在终端进行访问


--
8-26

./client 172.100.20.72


--
8-29

类的点方法使用只能够调用类方法,不能去调变量


--
8-30

-(IBAction)backgroundClick : (id)sender
{
[nameField resignFirstResponder];
[numberField resignFirstResponder];

//
if (numberField.text != nil)
{
NSLog(@"here");
aslider.value = (float)[numberField.text intValue] ;
float ff = (float)[numberField.text intValue];
NSLog(@"value is %d, %d, ff = %d, ff = %f", [numberField.text intValue], (int)(aslider.value), ff, ff);

// [self slideChange: aslider];
}
}

注意类型转换

在NSLog printf 中,%d %f 使用不当,即没有正确使用应该使用的类型格式,会导致整个输出结果受到影响,包括后面的输出


可以通过设置滑动条的value的值改变滑条的位置


--
8-31
注意,逻辑运算的结果只能是0 1 a && b a || b !a a < b
不要和位运算混淆 a & b a | b ~a


--
9-1

Stu * tt = [[[Stu alloc]init ] autorelease];
NSLog(@"tt count= %d", [tt retainCount]);



Person * pp = [[Person alloc]init] ;
NSLog(@"pp.stu count = %d", [pp.stu retainCount]);

pp.stu = tt;//[[Stu alloc] autorelease];

p3 = pp.stu;
// NSLog(@"pp.stu count = %d", [pp.stu retainCount]);
NSLog(@"tt count= %d", [tt retainCount]);


#import <Foundation/Foundation.h>
#import "Stu.h"

@interface Person : NSObject {

Stu* stu;
}

@property (retain) Stu* stu;
@end


pp.stu; 这个函数会增加retainCount的值 ++




   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值