Obj-c4[NSString, NSArray]

#import <Foundation/Foundation.h>

#import "Person.h"

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

//        //常量字符串NSString
//        NSString * str = @"蓝欧";
//        //NSLog(@"%p",str);//0x100001038
//        NSLog(@"%@ 科技",str);
//        NSString * str = @"lanou";
//        NSLog(@"%p",str);
//        NSLog(@"%@",str);
//        int n = 17;
//        CGFloat avg = 99.99;
//        NSString * str = [[NSString alloc] initWithFormat:@"lanou%d班 ,平均数%.2f",n,avg];
//        NSLog(@"%p",str);
//        NSLog(@"%@",str);

//最重要!-  便利构造器不需要管内存
//        NSString * str = [NSString stringWithFormat:@"%d蓝鸥共%d人",17,35];
//        NSLog(@"%@",str);
//        NSLog(@"%p",str);
    
//    NSString * str = @"河南 17 班";
//        NSUInteger len = [str length];
//        NSLog(@"%lu",len);
    
//    NSString * str = @"lanou";
//        NSString * newStr = [str stringByAppendingString:@"keji"];
//        NSLog(@"%@",str);
//        NSLog(@"%@",newStr);
    
//        int a = 17;
//        NSString * str = @"lanou";
//        NSString * s = [str stringByAppendingFormat:@"\t河南%dcalss",a];
//        NSLog(@"%@",s);
    
//    NSString * str = @"lanoukeji";
        //从某个位置开始到结束,求子串
       // NSString * s = [str substringFromIndex:5];
        
//        NSString * s = [str substringToIndex:5];
//        NSLog(@"%@",s);
        
//    NSString * str = @"lanoukeji";
//       // NSRange range = {3,4};
//       // NSRange range = NSMakeRange(3, 4);
//        
//       //NSString * str1 = [str substringWithRange:range];
//        NSString * str1 = [str substringWithRange:NSMakeRange(3, 4)];
//        NSLog(@"%@",str1);
    
    
//    NSString * str = @"www.baidu.com";
//        //根据传入的字符串把原串分割成几部分
//        NSArray * substrings = [str componentsSeparatedByString:@".b"];
//        NSLog(@"%@",substrings);
        
        //求子串在原串中的范围
//    NSString * str = @"lanouoakejioa";
//        NSRange  range = [str rangeOfString:@"oa"];
//        NSLog(@"location = %lu, length = %lu", range.location, range.length);
    
        
        
//        NSString * str = @"你是我的小呀小苹果";
//        //把原串中的所有出现某些子串替换成新串
//        NSString * str2 = [str stringByReplacingOccurrencesOfString:@"小" withString:@"大"];
//        NSLog(@"%@",str2);
        
        //检查是不是以子字符串打头的
//        NSString * str = @"http://www.baidu.com";
//        BOOL result = [str hasPrefix:@"http/"];
//       // NSLog(@"%d", result);
//        //BOOL输入YES,NO
//        NSLog(@"%@", result ? @"YES" :@"NO");
        
        //检查是不是以子字符串结尾的
//        NSString * str = @"http://www.baidu.com";
//        BOOL result = [str hasSuffix:@"cm"];
//        NSLog(@"%@",result ? @"YES" : @"NO");
        
        //比较两个字符串是否相等
//        NSString * str = @"asd";
//        NSString * str2 = @"a531d";
//        if ([str isEqualToString:str2]) {
//            NSLog(@"相等");
//        }
        
//        NSString * str = @"ASDASCDxZCv";
//        //返回一个字符串,把所有的大写改成小写
//        NSString * str1 = [str lowercaseString];
//        NSLog(@"%@",str1);
//        //返回一个字符串,把所有的小写改成大写
//        NSString * str = @"ASDASCDxZCv";
//        NSString * str1 = [str uppercaseString];
//        NSLog(@"%@",str1);
//        NSString * str = @"ASDASCDxZCv";
//        //返回一个字符串,把首字母大写,其余都小写
//        NSString * str1 = [str capitalizedString];
//        NSLog(@"%@",str1);
        
        //字符串装换基本类型
//        NSString * str = @"56.8";
//        NSString * str1 = @"26.9";
//        CGFloat str3 = [str floatValue] + [str1 floatValue];
//        NSLog(@"%.2f",str3);
        
        
//        NSMutableString * str = [NSMutableString stringWithFormat:@"蓝鸥"];
////        [str appendString:@"科技"];
//        [str appendFormat:@"科技"];
//       // NSRange a = {1,2};//删除一个字符串内容从给定的位置到指定的长度
//       // [str deleteCharactersInRange:a];
//        [str deleteCharactersInRange:NSMakeRange(1,2)];
//        //插入一个字符串从给定的位置
//        [str insertString:@"欧克" atIndex:1];
//        NSLog(@"%@",str);
//        //内容重置
//        [str setString:@""];
//        NSLog(@"str = %@", str);
        
//        NSString * str = @"abc";
//        NSString * str1 = @"aec";
//        //枚举类型                      //比较字符串,返回的时一个枚举类型的值
//        NSComparisonResult result = [str compare:str1];
//        NSLog(@"%ld",result);
        
        
        //创建数组,以nil标记字符串的结束
//        NSArray * array = [[NSArray alloc] initWithObjects:@"A",@"B",@"c",nil];
//        NSLog(@"%@",array);
       
//        NSArray * array = [NSArray arrayWithObjects:@"张山", @"李四", @"王武",nil];
//        NSLog(@"%@",array);
//        //根据下标索引数据
//        NSString * str = [array objectAtIndex:2];
//        NSLog(@"str = %@",str);
        
    
        Person * p1 = [[Person alloc] initWithName:@"张山" sex:@"男" age:25];
        Person * p2 = [[Person alloc] initWithName:@"李四" sex:@"男" age:54];
        Person * p3 = [[Person alloc] initWithName:@"杨紫" sex:@"女" age:25];
        NSArray * array = [NSArray arrayWithObjects:p1, p2, p3, nil];
//        NSLog(@"%@", array);
        
//        Person * per = [array objectAtIndex:2];//输出元素指定下标元素
//        NSLog(@"%@",per);
//        NSUInteger count = [array count];//查看数组元素个数
//        NSLog(@"%ld",count);
        
        //循环遍历输出数组元素
//        for (int i= 0; i < [array count] ; i++) {
//            Person * pr = [array objectAtIndex:i];
//            NSLog(@"%@", pr);
//        }
        
//        Person * p = [array firstObject];
//        Person * p0 = [array lastObject];
//        NSLog(@"%@", p0);
        
    
//        BOOL result = [array containsObject:p1];
//        NSLog(@"%@", result ? @"YES" : @"NO");
        
        
//        NSUInteger ps = [array indexOfObject:p2];
//        NSLog(@"%lu", ps);
        
//        NSArray * array2 =[NSArray arrayWithObjects:p1, p3, p2, nil];
//        BOOL result = [array isEqualToArray:array2];
//        NSLog(@"%@", result ? @"YES" : @"NO");

#import "Person.h"

@implementation Person

- (id)initWithName:(NSString *)name
                 sex:(
NSString *)sex
                 age:(
NSInteger)age
{  
self = [super init];
    if (
self) {
       
_name = name;
       
_sex = sex;
       
_age = age;
    }
    return
self;
}

//重写从父类继承过来的description方法,这个方法用于控制使用%@打印
//返回什么样的字符串,使用%@打印该类型对象,就会打印出什么内容
- (
NSString *)description{
return [
NSString stringWithFormat:@"%@ %@ %ld", _name, _sex, _age];

}

#import <Foundation/Foundation.h>

@interface Person : NSObject
{
    NSString * _name;
    NSString * _sex;
    NSInteger  _age;
}
- (id)initWithName:(NSString *)name
                 sex:(NSString *)sex
                 age:(NSInteger)age;

@end

时间: 2024-10-13 06:47:36

Obj-c4[NSString, NSArray]的相关文章

NSData NSDate NSString NSArray NSDictionary 相互转化

//    NSData  NSDate NSString NSArray NSDictionary json NSString *string = @"hello word"; NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; NSArray *array = [NSArray arrayWithObjects:@"1",@"2",@"3",nil

NSString NSArray和可变字符串、数组的用法

// 使用实例方法创建NSString对象 NSString *string1 = [[NSStringalloc]initWithFormat:@"姓名流年"]; NSLog(@"%@", string1); NSString *string2 = [[NSStringalloc]initWithFormat:@"Ming"]; NSLog(@"%@", string2); NSString *string3 = [[NSS

NSData NSDate NSString NSArray NSDictionary 相互转换

// NSData NSDate NSString NSArray NSDictionary json NSString *string = @"hello word"; NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; NSArray *array = [NSArray arrayWithObjects:@"1",@"2",@"3",nil]; N

继承NSString,NSArray 怎么写

今天在进行一个字符串测试的时候,自定义了一个继承自NSString的类,但是出现了一些问题,查阅官方文档后得到了下面这些结论 在Objective-C中,对于NSString.NSArray等一些系统的基础类,苹果公司不推荐继承创建新类的,要想给它们扩展功能只能给它们添加新的categroy(类目). 但是直接继承的话,也是可行的,只是会出现各种问题 以NSString为例 首先,创建一个NSString的子类 BFString,不添加任何方法和属性 直接创建一个BFString的实例对象,是没

OC4-foundation NSString NSArray

目标:1.如何查询API 习惯, 2.foundation 框架,nsstring nsarry nsnumber NSdictionary, 即时通讯:融云 环信. 1.API application 应用程序:interface 开放接口 (应用程序编程接口) 接口就是一系列的操作方法, 查询API作用: 能够知道都有些那些方法,每个属性的意义,每个方法的作用,这个类的继承关系. 查询方法:command +鼠标左键,点击类名 只能查看属性和方法: (2)alt/option  +鼠标左键,

NSString,NSArray和NSDictionary to JSON标准格式字符串

#import <Foundation/Foundation.h> @interface NSString (HXAddtions) +(NSString *) jsonStringWithDictionary:(NSDictionary *)dictionary; +(NSString *) jsonStringWithArray:(NSArray *)array; +(NSString *) jsonStringWithString:(NSString *) string; +(NSStr

Fouandation(NSString ,NSArray,NSDictionary,NSSet) 中常见的理解错误区

Fouandation 中常见的理解错误区 1.NSString //快速创建(实例和类方法) 存放的地址是 常量区 NSString * string1 = [NSString alloc]initWithString:@“123”]; NSString * string3 = [NSString stringWithString:@“123”]; //格式化方法创建   存放地址是堆区 NSString * string2 = [NSString alloc]initWithFormat:@

NSString NSArray

int main(int argc, const char * argv[]) { // // // SDK // Software Development Kit // 软件开发 // API // Application Programming Interface // 应用程序接口 /* 不可变字符串 Nsstring 创建之后内容和长度不可改变 */ // 创建字符串 // NSString *str1 = [NSString alloc]initWithString:@"刘庆春&quo

OBC: NSString 与 NSArray 互转

1. NSString 拆分成 NSArray NSString *_string = @"1,2,3,4,5";NSArray *_arr = [_string componentsSeparatedByString:NSLocalizedString(@",", nil)]; 2. NSArray 转为 NSString NSArray *_arr = [[NSArray alloc] initWithObjects:@"1", @"