zoukankan      html  css  js  c++  java
  • [objective-c] 07

     

    关于字符串NSString 数组NSArray 字典NSDictionary的学习,本文主要以代码的形式展示了其一些常用的方法,掌握这些方法便可以对字符串 数组 字典有个大致的掌握。

    [代码展示]

    ======字符串======

    #import <Foundation/Foundation.h>

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

    {

        @autoreleasepool

        {

            //1. stringWithFormat

            int age = 5;

            NSString *string = [NSString stringWithFormat:@"我的年龄是%d", age];

            NSLog(@"%@", string);

            

        //2.c字符串转换为oc字符串

            char *p = "abc";

            NSString *ocString = [NSString stringWithCString:p encoding:NSUTF8StringEncoding];

            NSLog(@"%@", ocString);

      

        //3.length        

            NSString *one = @"ddd";

            NSLog(@"%lu", one.length);

            

            NSString *a = @"abc";

            NSString *b = @"abc";

            NSString *c = [NSString stringWithFormat:@"abc"];

            

            if (a == b)  // a和b指向常量池内同一块空间,地址相同 ,比较的不是内容,比较的是地址

            {

                NSLog(@"a == b");

            }

            if (a == c)

            {

                NSLog(@"a == c");

            }

            if ([a isEqualToString:c])

            {

                NSLog(@"a equal c");

            }

            

            

            NSString *d = @"abc";

            NSLog(@"%ld", [d compare:a]);

            

            NSString *fileName = @"iOS_45.doc";

            if ([fileName hasPrefix:@"iOS"])

            {

                NSLog(@"");

            }

            if ([fileName hasSuffix:@".doc"])

            {

                NSLog(@"");

            }

            

            NSRange range = [fileName rangeOfString:@"456"]; // 找不到时

            NSLog(@"%lu, %lu", range.location, range.length);

            

            /*

            NSString *s = [fileName uppercaseString]; // 返回转换后的字符串,不会改变原字符串内容  NSString为不可变字符串

            NSLog(@"%@, %@", fileName, s);

            NSLog(@"%p", fileName);

            

            fileName = [fileName uppercaseString]; // 指向了新的字符串,

            NSLog(@"%@", fileName);

            NSLog(@"%p", fileName);

            

            NSMutableString *mString = [NSMutableString stringWithFormat:@"abcdefghijklmnopqrstuvwxyz"];

    //        NSMutableString *mm = [NSMutableString stringWithCapacity:10]; // 容量是10,存10个字符串时效率最高

            

            [mString appendString:@"opq"];

            NSLog(@"%@", mString);

            

            [mString insertString:@"ffff" atIndex:3];

            NSLog(@"%@", mString);

            

            NSRange range1 = NSMakeRange(0, 1);

            [mString deleteCharactersInRange:range1]; // 删除

             NSLog(@"%@", mString);

            

            [mString replaceCharactersInRange:range1 withString:@"123"]; // 替换

            NSLog(@"%@", mString);

            

            [mString substringWithRange:range1]; // 截取

             NSLog(@"%@", mString);

            

            // 将不可变String转换成可变字符串

            NSString *name = @"张三";

            NSMutableString *mname = [name mutableCopy];

            [mname replaceCharactersInRange:range1 withString:@"123"]; // 替换

            NSLog(@"%@", mname);

            

            */

            

        }

        return 0;

    }

    ======数组======

    ==Student类的声明==

    #import <Foundation/Foundation.h>

    @interface Student : NSObject

    @property (strong, nonatomic) NSString *name;

    @property (assign, nonatomic) int age;

    -(instancetype) initStudentWithName:(NSString *)vname Age:(int)vage;

    -(NSComparisonResult) compare:(Student *) other;

    @end

    ==Student类的实现==

    #import "Student.h"

    @implementation Student

    -(instancetype)initStudentWithName:(NSString *)vname Age:(int)vage

    {

        if (self = [super init])

        {

            self.name = vname;

            self.age = vage;

        }

        return self;

    }

    -(NSComparisonResult)compare:(Student *)other

    {

        return [self.name compare:other.name];

    }

    @end

    ======main======

    #import <Foundation/Foundation.h>

    #import "Student.h"

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

    {

        @autoreleasepool

        {

            // 只能存对象,不能存基本数据类型

            

            NSArray *array1 = @[@"one", @"two", @"three"]; // 提倡第一种方式

    //        NSArray *array2 = [NSArray arrayWithObjects:@"one", @"two", @"three", nil];

            

            for (int i = 0; i < array1.count; i++)

            {

                NSString *s1 = array1[i];

                NSString *s2 = [array1 objectAtIndex:i];

                NSLog(@"%@, %@", s1, s2);

            }

            

            

    //        NSMutableArray 是 NSArray子类

            

    //        NSMutableArray *marray1 = [NSMutableArray arrayWithCapacity:10]; // 空的数组

            NSMutableArray *marray2 = [NSMutableArray arrayWithObjects:@"one", @"two", @"three", nil];

            

            // 添加

            [marray2 addObject:@"123"];

            NSLog(@"%@", marray2);

            

            

            // 快速枚举

            for (id sub in marray2)

            {

                NSLog(@"%@", sub);

            }

            NSLog(@"===============");

            

            

            // 删除

            [marray2 removeObjectAtIndex:1];

            for (id sub in marray2)

            {

                NSLog(@"%@", sub);

            }

            NSLog(@"===============");

            

            

            // 替换

            [marray2 replaceObjectAtIndex:1 withObject:@"3"];

            for (id sub in marray2)

            {

                NSLog(@"%@", sub);

            }

            NSLog(@"===============");

            

            // 插入

            [marray2 insertObject:@"1" atIndex:2];

            for (id sub in marray2)

            {

                NSLog(@"%@", sub);

            }

            NSLog(@"===============");

            

            

            

            // 排序

            Student *s1 = [[Student alloc] initStudentWithName:@"张e三" Age:12];

            Student *s2 = [[Student alloc] initStudentWithName:@"李四" Age:15];

            Student *s3 = [[Student alloc] initStudentWithName:@"王五" Age:14];

            

            NSMutableArray *stds = [NSMutableArray arrayWithCapacity:3];

            

            [stds addObject:s1];

            [stds addObject:s2];

            [stds addObject:s3];

            

            

            // 1 通过排序描述对象完成排序

            NSSortDescriptor *desc = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];

            [stds sortUsingDescriptors:@[desc]];

            for (id std in stds)

            {

                NSLog(@"name=%@, age=%d", [std name], [std age]);

            }

            NSLog(@"===============");

            

            

            // 2 通过代码块的方式完成排序

            [stds sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {

                if ([obj1 age] < [obj2 age])

                {

                    return -1;

                }

                if ([obj1 age] > [obj2 age])

                {

                    return 1;

                }

                return 0;

                

            }];

            for (id std in stds)

                        {

                            NSLog(@"name=%@, age=%d", [std name], [std age]);

                        }

            NSLog(@"===============");

      

            

            

            [stds sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {

                return [[obj1 name] compare:[obj2 name]];

            }];

            for (id std in stds)

            {

                NSLog(@"name=%@, age=%d", [std name], [std age]);

            }

            NSLog(@"===============");

            

            

            // 3 通过对象的排序方法完成排序

            

            [stds sortUsingSelector:@selector(compare:)];

            for (id std in stds)

            {

                NSLog(@"name=%@, age=%d", [std name], [std age]);

            }

            NSLog(@"===============");

        }

        return 0;

    }

    ======字典======

    #import <Foundation/Foundation.h>

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

    {

        @autoreleasepool

        {

            // 字典的使用

            // 包装 将基本的数据类型包装成对象

            NSNumber *num1 = [NSNumber numberWithInt:12];

            NSDictionary *dic = @{@"name":@"张三", @"age":num1};

    //        NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"张三", @"name", num1, @"age", nil];

         

            

            // 遍历字典

            // 1 得到所有的key

            NSArray *keys = [dic allKeys];

            for (id key in keys)

            {

                id number1 = dic[key]; // 提倡用这种方式

    //            id number2 = [dic valueForKey:key];

                if ([key isEqualToString:@"name"])

                {

                    NSLog(@"%@", (NSString *) number1);

                }

                else

                {

                    // 拆箱 获得number对象所存储的基本数据类型

                    NSNumber *n = (NSNumber *) number1;

                    NSLog(@"%d", [n intValue]);

                }

           

            }

            

            

            // 2 快速枚举 相当于在字典的key中做遍历,得到所有的key

            for (id key in dic)

            {

                NSLog(@"%@", dic[key]);

            }

            

    #pragma mark

    //        NSMutableDictionary

            

            ///////////////////////////////////////////////

            NSDictionary *s1 = @{@"sid":@"1", @"sname":@"张三", @"sage":@"12", @"sex":@"男"};

            NSDictionary *s2 = @{@"sid":@"2", @"sname":@"李四", @"sage":@"13", @"sex":@"女"};

            NSDictionary *s3 = @{@"sid":@"3", @"sname":@"王五", @"sage":@"13", @"sex":@"男"};

            

            NSMutableArray *array = [NSMutableArray arrayWithCapacity:3];

            

            [array addObject:s1];

            [array addObject:s2];

            [array addObject:s3];

            

            /*

             类 Tool

            

            +(Dictionary *) dictionary for URL :(NSString *) url;

            

            截出字符串

            按某一个符号分割

            +

            */

        }

        return 0;

    }

    ======运行结果======

    张三

    12

    张三

    12

  • 相关阅读:
    OpenGL编程 基础篇(二)Sierpinski垫片
    PAT1045. Favorite Color Stripe
    OpenGL编程 基础篇(一)
    百练1145:Tree Summing
    百练1745:Divisibility
    百练1321:棋盘问题
    Scrapy架构及其组件之间的交互
    MongoDB快速入门
    Redis快速入门
    Memcache所有方法及参数详解
  • 原文地址:https://www.cnblogs.com/lqios/p/4288261.html
Copyright © 2011-2022 走看看