oc j基础

和果子

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅  :: 管理
    9 Posts :: 0 Stories :: 1 Comments :: 0 Trackbacks

公告

昵称:和果子
园龄:1年6个月
粉丝:6
关注:3

+加关注

搜索

常用链接

我的标签

随笔档案

阅读排行榜

评论排行榜

推荐排行榜

OC基础第四讲--字符串、数组、字典、集合的常用方法

字符串、数组、字典、集合有可变和不可变之分。以字符串为例,不可变字符串本身值不能改变,必须要用相应类型来接收返回值;而可变字符串调用相应地方法后,本身会改变;可变字符串是不可变字符串的子类。以下是常见的方法,其他方法可通过苹果帮助文档(API)查询使用。

1.1不可变字符串NSString

//  使用实例方法创建NSString对象

NSString *string1 = [[NSString alloc] initWithFormat:@"姓名41564564"];

NSLog(@"%@", string1);

NSString *string2 = [[NSString alloc] initWithFormat:@"机构名称: %@  成立时间: %d", @"蓝鸥", 2001];

NSLog(@"%@", string2);

//  使用类方法创建一个NNString对象

NSString *string3 = [NSString stringWithFormat:@"姓名 :%@ 年龄 :%d", @"小王", 20];

NSLog(@"%@", string3);

//  直接赋值

NSString *string4 = @"hello WORLD my baby";

NSLog(@"%@", string4);

//  获取字符串长度

NSUInteger string1Length = [string1 length];

NSLog(@"%ld", string1Length);

//  判断字符串是否有指定前缀

BOOL result1 = [string1 hasPrefix:@"姓名"];

NSLog(@"%d", result1);

//  后缀

BOOL result2 = [string2 hasSuffix:@"2011"];

NSLog(@"%d", result2);

//  查找字符串位置

NSRange range1 = [string2 rangeOfString:@"蓝鸥"];

NSLog(@"location : %ld  length : %ld", range1.location, range1.length);

//  分割字符串

NSString *subStirng1 = [string2 substringFromIndex:3];

NSLog(@"%@", subStirng1);

NSString *subString2 = [string2 substringToIndex:3];

NSLog(@"%@", subString2);

NSRange range2 = NSMakeRange(10, 2);// 建一个NSRange的结构体,注意越界

NSString *subString3 = [string2 substringWithRange:range2];

NSLog(@"%@", subString3);

//  拼接

NSString *string5 = [string2 stringByAppendingString: @"hello"];

NSLog(@"%@", string5);

//  替换

NSString *string6 = [string2 stringByReplacingOccurrencesOfString:@"蓝鸥" withString:@"海鸥"];

NSLog(@"%@", string6);

NSRange range3 = NSMakeRange(3, 3);

NSString *string7 = [string2 stringByReplacingCharactersInRange:range3 withString:@"HAHA 你中招了"];

NSLog(@"%@", string7);

//  比较

//  比较是否相同

BOOL isEqual = [string4 isEqualToString:@"hello"];

NSLog(@"%d", isEqual);

//  比较大小

NSComparisonResult result3 = [string2 compare:string4];

NSLog(@"%ld", result3);

//  大写<->小写

NSString *string8 = [string4 lowercaseString];

NSLog(@"%@", string8);

NSString *string9 = [string4 uppercaseString];

NSLog(@"%@", string9);

NSString *string10 = [string4 capitalizedString];

NSLog(@"%@", string10);

//  字符串和数值类型转换

NSInteger intStr = [@"124" integerValue];

NSLog(@"%ld", intStr);

double doubleStr = [@"1000.00432" doubleValue];

NSLog(@"%lf", doubleStr);

       1.2 可变字符串NSMutableString

//   创建可变字符串

NSMutableString *mutableStr1 = [[NSMutableString alloc] initWithFormat:@"蓝鸥科技有限公司"];

NSMutableString *mutableStr2 = [NSMutableString stringWithFormat:@"蓝鸥科技有限公司"];

NSLog(@"%@, %@", mutableStr1, mutableStr2);

//  字符串拼接(在原串的基础上)

[mutableStr1 appendString:@".txt"];

NSLog(@"%@", mutableStr1);

//  插入(在下标的后一位插入)

[mutableStr1 insertString:@"3G" atIndex:2];

NSLog(@"%@", mutableStr1);

//  删除

[mutableStr1 deleteCharactersInRange:NSMakeRange(2, 2)];

NSLog(@"%@", mutableStr1);

   2.1 不可变数组NSArray

NSArray数组类(不可变)

//  1.使用实例方法创建数组

NSArray *array1 = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", nil];//  nil不能省

NSLog(@"%@", array1); // 可以直接打印数组

//  2.使用类方法创建数组

NSArray *array2 = [NSArray arrayWithObjects:@"4", @"5", @"6", nil];

NSLog(@"%@", array2);

//  3.获取数组元素个数

NSUInteger count = [array1 count];

NSLog(@"%ld", count);

//  4.根据索引值获取对象(根据方法的返回值来确定用什么类型来接收)

NSString *p = [array1 objectAtIndex:1];

NSLog(@"%@", p);

//  5.获取对象在数组中的索引值

NSUInteger index = [array1 indexOfObject:@"3"];

NSLog(@"index : %ld", index);

1.2可变数组 NSMutableArray

//  1. 使用实例方法创建

NSMutableArray *mutableArray1 = [[NSMutableArray alloc] initWithObjects:@"aa",@"bb", @"cc", nil];

NSLog(@"%@", mutableArray1);

//  2. 使用类方法创建

NSMutableArray *mutableArray2 = [NSMutableArray arrayWithObjects:@"aaa",@"bbb", @"ccc", nil];

NSLog(@"%@", mutableArray2);

//  3. 添加元素

[mutableArray1 addObject:@"dd"];

NSLog(@"%@", mutableArray1);

//  4. 插入元素

[mutableArray1 insertObject:@"haha" atIndex:1];

NSLog(@"%@", mutableArray1);

//  5. 删除元素(通常根据索引或者对象值)

[mutableArray1 removeObject:@"haha"];

[mutableArray1 removeObjectAtIndex:1];

NSLog(@"%@", mutableArray1);

//  6. 替换元素

[mutableArray1 replaceObjectAtIndex:2 withObject:@"DD"];

NSLog(@"%@", mutableArray1);

//  7. 根据索引值交换指定位置的两个元素

[mutableArray1 exchangeObjectAtIndex:1 withObjectAtIndex:2];

NSLog(@"%@", mutableArray1);

//  8. 很据对象交换两个元素的位置(先找到对象的索引值)

[mutableArray1 exchangeObjectAtIndex: [mutableArray1 indexOfObject:@"cc"] withObjectAtIndex:[mutableArray1 indexOfObject:@"DD"]];

NSLog(@"%@", mutableArray1);

//  9. 遍历数组对象

使用for循环遍历

for (int i = 0; i < [mutableArray1 count]; i ++) {

NSString *str = [mutableArray1 objectAtIndex:i];

NSLog(@"遍历结果:%@", str);

}

使用for in快速遍历

for (NSString *str in mutableArray1) {

NSLog(@"快速遍历:%@", str);

}

数组中使用枚举器(for in 快速遍历的实质)

NSArray *arr = [NSArray arrayWithObjects:@"abc", @"hdf", @"hshah", nil];

NSEnumerator *enumerator2 = [arr objectEnumerator];

NSString *str2 = nil;

while (str2 = [enumerator2 nextObject]) {

NSLog(@"str2: %@", str2);

}

3.NSNumber (基本数据类型不能放在数组中,解决这个问题,可以把数字封装成对象)

//  1.创建一个数组对象

NSMutableArray *array = [NSMutableArrayarrayWithObjects:@"1", @"2", nil];

//  2.用NSNumber对Integer进行包装

NSNumber *intNumber = [NSNumber numberWithInteger:3];

//  3.将包装后烦人NSNumber对象放到数组中

[array addObject:intNumber];

NSLog(@"%@", array);

//  4.根据下标取出对象,用NSNumber类型进行接收

NSNumber *a = [array objectAtIndex:2];

NSLog(@"%@", a);

//  5.把NSNunber转成Integer类型

NSInteger integerA = [a integerValue];

NSLog(@"%ld", integerA);

4.1 NSDictionary 不可变字典

//  key通过哈希算法算出一个数作为一个索引下标把值存到相应的的位置在内存中是散列结构,即无序

//  使用实例方法创建(键值对的形式) 键和值都必须是对象

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", @"v3", @"k3", @"v5", @"k5", @"v4", @"k4", @"value6", @"key6", @"value7", @"ke7", nil];

NSLog(@"%@", dictionary);

//  使用类方法创建

NSDictionary *dictionary2 = [NSDictionary dictionaryWithObjectsAndKeys:@"v1", @"k1", @"v2", @"k2", nil];

NSLog(@"%@", dictionary2);

NSDictionary *dictionary3 = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];

NSLog(@"%@", dictionary3);

NSArray *keyArray = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil]; //  存放键的数组

NSArray *valueArray = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil];//  存放值的数组

NSDictionary *dictionary4 = [NSDictionary dictionaryWithObjects: keyArray forKeys:valueArray];

NSLog(@"%@", dictionary4);

// 创建空字典

NSDictionary *dic = [NSDictionary dictionary];

//  使用一个文件创建字典对象 新建文件步骤:command + N -> Resource -> Property List

NSDictionary *dictionary5 = [NSDictionary dictionaryWithContentsOfFile:@"/Users/lanou3g/Desktop/OC/lesson5-20140425/lesson5-20140425/dictionary.plist"];

NSLog(@"%@", dictionary5);

//  返回字典中键值对的个数

NSLog(@"%ld", [dictionary5 count]);

//  字典取值 获取指定key对应的value

NSString *value1 = [dictionary5 objectForKey:@"ly"];

NSLog(@"%@", value1);

//  返回所有的key数组

NSArray *allKeysArray = [dictionary5 allKeys];

NSLog(@"%@", allKeysArray);

//  返回所有的value数组

NSArray *allValuesArray = [dictionary5 allValues];

NSLog(@"%@", allValuesArray);

//  使用key枚举器(获取所有key)

NSEnumerator *enumerator = [dictionary5 keyEnumerator];

NSString *str = nil;

while (str = [enumerator nextObject]) {

NSLog(@"%@", str);

}

 4.2创建可变字典 NSMutableDictionary

NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1", @"k1", @"v2", @"k2", nil];

NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"v3", @"k3", @"v4", @"k4", @"v5", @"k5", nil];

//  用于整理对象的拼接

[dic1 addEntriesFromDictionary:dic2];

NSLog(@"%@", dic1);

//  删除字典中某个对象

[dic1 removeObjectForKey:@"k1"];

NSLog(@"%@", dic1);

//  删除字典全部对象

[dic1 removeAllObjects];

NSLog(@"%@", dic1);

//  设置字典

[dic1 setDictionary:dic2];

NSLog(@"%@", dic1);

 5.1NSSet集合对象 容器类

//  1. 使用类方法创建对象

NSSet *set1 = [NSSet set];  //  创建一个空的集合对象

NSSet *set2 = [NSSet setWithObject:@"abc"];

NSSet *set3 = [NSSet setWithObjects:@"abc", @"aaa", @"bbb", nil];

NSLog(@"%@", set3);

NSArray *array = [NSArray arrayWithObjects:@"a",@"b", @"c", nil];

NSSet *set4 = [NSSet setWithArray:array];   //  使用数组创建

NSLog(@"%@", set4);

NSSet *set5 = [NSSet setWithSet:set4];      //  使用集合创建

NSLog(@"%@", set5);

//  2.使用实例方法创建

NSSet *set6 = [[NSSet alloc] init];

NSLog(@"%@", set6);

NSSet *set7 = [[NSSet alloc] initWithObjects:@"hello", @"hhaa", @"bbjdh", nil];

NSLog(@"%@", set7);

NSSet *set8 = [[NSSet alloc] initWithArray:array];

NSLog(@"%@", set8);

NSSet *set9 = [[NSSet alloc] initWithSet:set7];

NSLog(@"%@", set9);

//  3.返回几个元素个数

NSLog(@"%ld", [set7 count]);

//  4.枚举器访问集合元素

NSEnumerator *enumerator = [set7 objectEnumerator];

NSString *str = nil;

while (str = [enumerator nextObject]) {

NSLog(@"%@", str);

}

//  5.判断两个几个是否有交集

BOOL ifhasIntersection = [set2 intersectsSet:set3];

NSLog(@"%d", ifhasIntersection);

//  6.判断两个集合是否相等

NSLog(@"%d", [set2 isEqualToSet:set3]);

//  7.判断当前集合是否是子集

NSLog(@"%d", [set2 isSubsetOfSet:set3]);

5.2可变集合 NSMutableSet

//  创建指定元素个数的一个集合对象

NSMutableSet *mutableSet = [NSMutableSet setWithCapacity:4];

[mutableSet addObject:@"aaa"];

NSLog(@"%@", mutableSet);

//  类方法创建可变集合

NSMutableSet *mutableSet1 = [NSMutableSet setWithObjects:@"aaa", @"bbb", @"ccc", nil];

NSMutableSet *mutableSet2 = [NSMutableSet setWithObject:@"aaa"];

//  添加一个对象到集合

[mutableSet2 addObject:@"ddd"];

NSLog(@"%@", mutableSet2);

//  从集合中删除一个对象

[mutableSet2 removeObject:@"ddd"];

NSLog(@"%@", mutableSet2);

//  把数组对象添加到集合对象中

NSArray *arr = [NSArray arrayWithObjects:@"eee", @"fff", nil];

[mutableSet1 addObjectsFromArray:arr];

NSLog(@"%@", mutableSet1);

//  得到两个集合的交集 注意intersectSet和intersectsSet的区别,后者是判断是否有交集的方法, 返回的是bool值

[mutableSet1 intersectSet:mutableSet2];

NSLog(@"%@", mutableSet1);

//  从一个集合中减去另一个集合

[mutableSet1 minusSet:mutableSet2];

NSLog(@"%@", mutableSet1);

//  从一个元素中删除所有元素

[mutableSet2 removeAllObjects];

NSLog(@"%@", mutableSet2);

//  取两个集合的并集

[mutableSet1 unionSet:mutableSet2];

NSLog(@"%@", mutableSet1);

NSLog(@"%@", mutableSet1);

//  设置给集合赋值

[mutableSet1 setSet:mutableSet2];

NSLog(@"%@", mutableSet1);

关于数组排序

NSArray *array = [NSArrayarrayWithObjects:@"12", @"454", @"7878", @"77122", @"555", @"9", nil];

NSLog(@"排序前:%@", array);

//  这样排序是按照字符串大小来排序的不是数值排序

array = [array sortedArrayUsingSelector:@selector(compare:)];

NSLog(@"排序后:%@", array);

//  进行数值排序需要用以下的方法

array = [array sortedArrayUsingFunction:intSort context:NULL];

NSLog(@"排序后:%@", array);

以下是intSort 的方法实现:

NSInteger intSort(id num1, id num2, void *context)

{

int v1 = [num1 intValue];

int v2 = [num2 intValue];

if (v1 < v2)

returnNSOrderedAscending;

else if (v1 > v2)

returnNSOrderedDescending;

else

returnNSOrderedSame;

}

关于字典与数组

/// 字典里可以存数组,数组可以存字典

NSArray *arr = [NSArray arrayWithObjects: @"1", @"2", @"3", nil];

//  把数组arr放在字典dic里

NSDictionary *dic = [NSDictionary dictionaryWithObject:arr forKey:@"array"];

NSLog(@"%@", dic);

//  把字典dic和数组arr 放在数组arr2里

NSArray *arr2 = [NSArray arrayWithObjects:arr, dic, nil];

NSLog(@"%@", arr2);

标签: oc零基础面向对象字符串字典

好文要顶 关注我 收藏该文  

和果子
关注 - 3
粉丝 - 6

+加关注

0

0

(请您对文章做出评价)

«上一篇:OC基础第二讲
»下一篇:OC基础第五讲--Block、数组高级、字面量

posted on 2014-04-26 20:31 和果子 阅读(597) 评论(0)  编辑 收藏

抱歉!发生了错误!麻烦反馈至[email protected]

刷新评论刷新页面返回顶部

发表评论

昵称:

评论内容:

     

注销 订阅评论

[Ctrl+Enter快捷键提交]

【推荐】50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
【推荐】融云即时通讯云-专注为 App 开发者提供IM云服务
【福利】极光推送-富媒体+大数据驱动精准推送,完全免费开放啦

最新IT新闻:
·  百度联手复旦发布全国医院排行榜
·  笼罩雾霾的行星上更可能有外星生命
·  电击戒“网瘾”还不够 美国人发明“荒野治疗法”
·  《大西洋月刊》撰稿人:程序员不配叫工程师
·  它看起来是一条黑线,其实是电话、手环、台灯……
» 更多新闻...

最新知识库文章:

·  被误解的MVC和被神化的MVVM
·  再谈设计和编码
·  什么时候应该避免写代码注释?
·  持续集成是什么?
·  人,技术与流程

» 更多知识库文章...

Copyright @ 和果子
Powered by: .Text and ASP.NET 
Theme by: .NET Monster

时间: 2024-08-09 02:21:00

oc j基础的相关文章

OC语言基础知识

OC语言基础知识 一.面向对象 OC语言是面向对象的,c语言是面向过程的,面向对象和面向过程只是解决问题的两种思考方式,面向过程关注的是解决问题涉及的步骤,面向对象关注的是设计能够实现解决问题所需功能的类. 术语:OO面向对象.OOP面向对象编程 二.类 (一)关于类 类的设计只关注三个东西:类名.属性和方法 注意:一般名词都是类,拥有相同属性和行为的对象都可以抽象为一个类,类名是标识符的一种,需要符合规范,通常类名的第一个字母大写,且不能有下划线,如果有多个单词则使用驼峰标识.在对方法进行类的

李洪强iOS开发之OC语言基础知识

OC语言基础知识 一.面向对象 OC语言是面向对象的,c语言是面向过程的,面向对象和面向过程只是解决问题的两种思考方式,面向过程关注的是解决问题涉及的步骤,面向对象关注的是设计能够实现解决问题所需功能的类. 术语:OO面向对象.OOP面向对象编程   二.类 (一)关于类 类的设计只关注三个东西:类名.属性和方法 注意:一般名词都是类,拥有相同属性和行为的对象都可以抽象为一个类,类名是标识符的一种,需要符合规范,通常类名的第一个字母大写,且不能有下划线,如果有多个单词则使用驼峰标识.在对方法进行

IOS学习之路--OC的基础知识

运行过程 1.编写OC程序:.m源文件 2.编译.m文件为.o目标文件:cc -c xxxx.m 3.链接.o文件为a.out可执行文件:cc xxxx.o -framework Foundation 4.执行a.out文件:./a.out #import 的功能跟#include一样,只是更好用,他避免了头文件的多次包含 为了能使用OC的特性, 一定要引入#import <Foundation/Foundation.h> 类定义// @implementation 和 @end // 设计(

OC语言基础

.h(头文件) .m(源文件c.oc) .mm(源文件c.oc.c++) gcc -c      -o    汇编 -framework Foundation链接  NS开头 字符串对象加@ oc中完全兼容c的函数库 NSLog可以自动换行,并且提供文件名编译时间等 需要包含头文件,每一个框架都有一个主头文件,其名字和主头文件相同,主头文件包含了这个框架的所有头文件. #import可以自动防止头文件重复包含. 链接时,需要手动连接框架. 10.多文件编译:所有对象都是以指针形式存在:跟c类似.

C和OC的基础语法(易混淆的概念知识)

List 0. 深复制与浅复制, NSObject万能指针.id指针.instancetype区别,单例import.include.@class的区别 strong 与 weak 区别 #define  和  typedef的区别, static 与  extern 区别,@required与@optional 区 别,@private.@protected .@public.@package区别 变量的命名规则以及规范(4规则,2规范) 数据类型转换 printf与scanf,自增自减,逻辑

[转]站在OC的基础上快速理解Swift的类与结构体

(阅读此文章前,您已经有一定的Object-C语法基础了!) 2014年,Apple推出了Swift,最近开始应用到实际的项目中. 首先我发现在编写Swift代码的时候,经常会遇到Xcode不能提示,卡顿,直接闪退等问题,尤其是在Swift和OC混编时.(不知道其他开发者是否也有这样的经历,但是我相信这样的问题,很快会得到解决) 然后感觉Swift并不像网上很多朋友说的那样简单.有很多细节问题是值得注意的,甚至有很多思路是颠覆了传统的开发语言的!又有很多特性是结合了其他编程语言的优点! Swif

魏兆辉的IOS基础学习笔记之十四 OC语言基础-09 OC对象的内存管理

一. 原理 对于任何继承了NSObject的对象,都有一个与之关联的整形变量,称为引用计数器:只有计数器的值为0的时候,OC就回收该对象,否则永不回收. 1. 创建对象时,如使用alloc ,new , copy,引用计数器的值为1 2. 当使用release方法释放时,计数器的值减1 3. 当使用retain方法时,计数器的值加1 4. 当对象被回收时候,自动调用对象的dealloc方法, 5. 使用retainCount方法来获取引用计数器的值 二. 防止内存泄露标准示例写法: @class

魏兆辉的IOS基础学习笔记之十一 OC语言基础-06 Foundation框架介绍

在之前的博文中,我们创建的项目文件的时候,默认都有引用#import <Foundation/foundation.h> 这个头文件.但是,之前我们对Foundation都没有展开介绍.这篇博文,我们就揭开它神秘的面纱. 之前,我们有提到过OC使用的是Cocoa框架.那么Cocoa和Foundation之间,有啥关联呢?实际上,Cocoa是由许多个不同的框架组成的.最常用的是Foundation和Application Kit. Foundation框架中有很多有用的.面向数据的简单类和数据类

黑马程序员&mdash;&mdash;【黑马视频笔记】OC语言基础之利用property优化封装

  1.property功能用法 1: // @property:可以自动生成某个成员变量的setter和getter声明 2: @property int age;//可以直接免去变量的声明 3: //- (void)setAge:(int)age; 4: //- (int)age; .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New",