从零开始自学ios ---第1天 Foundation的一些总结

本文介绍了Objective-C中的基本数据结构如NSRange、CGPoint、CGSize、CGRect,并详细讲解了如何使用NSString及其常用方法,包括字符串创建、比较、查找前缀和后缀等。

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

#import <Foundation/Foundation.h>
int main(int argc,const char *argv[])
{
@autoreleasepool {
//insert code here
NSLOG(@"hello,World");
}
}
//结构体NSRange
typedef struct _NSRange{
unsigned int Location;
unsigned int Length;
}NSRange;
//Location 字段存放该字段的起始位置,Length:所含元素的个数
//创建新的NSRange有3种方式
NSRange range;
range.Location=17;
range.Length=4;
//2 聚合结构赋值机制
NSRange range=[17,4];
//3 Cocoa 提供的快捷函数 NSMakeRange();使用它可以在任何能够使用函数的地方使用它
//例如在方法调用中将其作为参数进行传递
[anObject flabulateWithRange:NSMakeRange(13,15)];
NSRange range=NSMakeRange(17,4);
//2集合数据类型 CG 由Core Graphics(制图)提供
//CGPoint 笛卡尔平面中的一个坐标 cocos2d也用到
struct CGPoint
{
float X;
float Y;
}
//CGSize 用来储存长度和宽度:
struct CGSize
{
float width;
float height;
}
//矩形数据类型 CGRect
struct CGRect
{
CGPoint origin;
CGSize size;
}
//Cocoa 也为我们提供了这些数据类型的快捷函数:CGPointMake(),CGSizeMake(),CGRectMake();
*///字符串NSString 的stringWithFormat:方法就是通过格式字符串和参数来创建的NSString
+(id)stringWithFormat:(NSString *)format,...;
//创建新字符串
NSString *height;
height=[NSString stringWithFormat:@" Your hegiht is %d feet,%d inches",5,11];
//+,就是把方法定义类方法(class method) 用于创建新的实例
NSColor * haveTheBules=[NSColor blueColor];
UIColor * blueman=[UIColor blueColor]
//NSSTring中另一个好用的方法是length,它返回的时字符串的字符个数
-(NSUInteger) Length;
//可以这样用它
NSUInteger Length=[height Length];
if ([height Length]>35)
{
NSLOG(@"wow,you're really tall!");
}
//字符串比较isEquaLToString: 其返回BOOL值 比较字符串内容是否相同
-(BOOL)isEqualToString:(NSString *)aString;
NSString *thing1=@"hello 5";
NSString *thing2=[NSString stringWithFormat:@"hello %d",5];
if([thing1 isEqualToString:thing2])
{
NSLOG(@"They are the same")
}
//要比较两个字符串,可以使用compare:方法
-(NSComoarionResult)compare:(NSString *)aString;
//compare:options:
-(NSComoarionResult) compare:(NSString *)aString
options:(NSStringCompareOptions)mask;
//NSCaseInsensitiveSrarch:不区分大小写字符
//NSLiteralSearch:进行完全比较,区分大小写字符
//NSNumericSearch:比较字符串个数,而不是字符串串值。
if([thing1 compare:thing2 options:NSCaseInsensitiveSrarch|NSNumericSearch]==NSOrderedSame)
{
NSLOG(@"They match!");
}
//检查字符串是否以另一个字符串开头,判断字符串是否以另一个字符串结尾
-(BOOL)hasPrefix:(NSString *)aString;
-(BOOL)hasSuffix:(NSString *)aString;
NSString*filename=@"draft-chapter.pages";
if([filename hasPrefix:@"draft"])
{
//this is a draft
}
if([filename hasSuffix:@".MOV"])
{
//
}
-(NSRange)rangeOfString:(NSString *)aString;//返回NSRange结构体
//可变性 NSMutableString
-(void) appendString:(NSString *)aString;
-(void)appendFormat:(NSString *)format,...;//增加字符串
NSMutableString*friends=[NSMutableString stringWithCapacity:50 ];
[friends appendString:@"James BethLynn Jack Evan"];
NSRange JackRange=[friends rangeOfString:@"Jack"];
JackRange.Length++;//
[friends deleteCharactersInRange:JackRange];
NSMutableString *string=[NSMutableString stringWithFormat:@"Jo%dy",2];
//集合大家族
NSArray *array2=@[@"one",@"two",@"three"];
-(NSUInteger)count;
-(id)objectAtIndex:(NSUInteger)index;
id*myObject=array1[1];
for(NSInteger i=0;i<[array count];i++)
{
NSLOG(@"index %d has %@.",i,[array objectAtIndex:i]);
}
for(NSInteger i=0;i<[array count];i++)
{
NSLOG(@"index %d has %@.",i,array[i]);
}
//可变数组
+(id)arrayWithCapacity:(NSUInteger)numTtems;
//循环加入4个轮胎
for (NSInteger i=0;i<4;i++)
{
Tire *tire=[Tire new];
[array addObject:tire];
}
//删除对象
-(void)removeObjectAtIndex:(NSUInteger)index;
[array removeObjectAtIndex:1];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值