oc程序代码

//

//  main.m

//  aa

//

//  Created by rcfwzx on 15/11/20.

//  Copyright (c) 2015年 rcfwzx. All rights reserved.

//

#import <Foundation/Foundation.h>

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

@autoreleasepool {

NSString *[email protected]"jjkkllFDSFDaqt";

NSString *[email protected]"atrGDSass";

//拼接

NSLog(@"--------拼接--------");

NSString *c=[a stringByAppendingString:b];

NSLog(@"拼接%@",c);

NSLog(@"拼接为%@\n\n",[a stringByAppendingString:b]);

//赋值

NSLog(@"--------赋值--------");

NSString *e=[[NSString alloc]init];

[email protected]"fflls";

NSLog(@"负值为%@\n\n",e);

//格式化

NSLog(@"-------格式化---------");

NSString *c1=[[NSString alloc]initWithString:[NSString stringWithFormat:@"%@",c]];

NSLog(@"%@\n\n",c1);

//判断是否相等

NSLog(@"------判断是否相等----------");

BOOL *c2=[a isEqualToString:b];

if(c2==0)

NSLog(@"不相等\n\n");

else  NSLog(@"相等\n\n");

NSLog(@"判断为%i\n\n",[a isEqualToString:b]);

//大写

NSLog(@"--------大写--------");

NSLog(@"大写%@",[a uppercaseString]);

//小写

NSLog(@"-------小写---------");

NSLog(@"小写%@",[a lowercaseString]);

//首字母大写

NSLog(@"-------首字母大写---------");

NSLog(@"首字母%@\n\n",[c capitalizedString]);

//长度

NSLog(@"-------长度---------");

unsigned long a1=[a length];

NSLog(@"字符%@的长度为%d\n\n",a,a1);

//从输入的数字后面开始到尾 显示

NSLog(@"------从输入的数字后面开始到尾 显示----------");

NSString *c4=[a substringFromIndex:4];

NSLog(@"%@\n\n",c4);

//从开始到输入的数字为止 显示

NSLog(@"-------从开始到输入的数字为止 显示---------");

NSString *c5=[a substringToIndex:5];

NSLog(@"%@\n\n",c5);

//从输入的第一个数字后面开始 显示第二数字的数量为止

NSLog(@"---------从输入的第一个数字后面开始 显示第二数字的数量为止-------");

NSString *c7=[a substringWithRange:NSMakeRange(4,3)];

NSLog(@"%@\n\n",c7);

//不可变字符串

NSLog(@"--------不可变字符串--------");

NSString *[email protected]"dsopliktkkgrt";

//可变字符串

NSLog(@"---------可变字符串-------");

NSMutableString *c9=[[NSMutableString alloc]initWithString:c8];

NSLog(@"可变字符串为%@\n\n",c9);

//插入字符串到另一个字符串

NSLog(@"--------插入字符串到另一个字符串--------");

[c9 insertString:@"ppl" atIndex:3];

NSLog(@"可变字符串为%@\n\n",c9);

//查看

NSLog(@"---------查看-------");

NSRange sub=[c9 rangeOfString:@"k"];

NSLog(@"查看%@\n\n",c9);

while (sub.location!=NSNotFound)

{

[c9 deleteCharactersInRange:sub];

sub=[c9 rangeOfString:@"k"];

}

if(sub.location!=NSNotFound)

{

NSLog(@"没找到\n\n");

}

//删除

NSLog(@"-------删除---------");

NSLog(@"删除%@\n\n",c9);

//重新设置字符串

NSLog(@"-------重新设置字符串---------");

[c9 setString:@"kkggpqwe"];

//替换/修改

NSLog(@"--------替换/修改--------");

[c9 replaceCharactersInRange:NSMakeRange(5,3) withString:@"k"];

NSLog(@"替换%@\n\n",c9);

//动态数组1

NSLog(@"--------动态数组1--------");

NSMutableArray *d3=[[NSMutableArray alloc]initWithObjects:@"dda",@"dde",nil];

//倒序

NSLog(@"--------倒序--------");

NSMutableString *nn=[[NSMutableString alloc]initWithString:@"ddrrhhq"];

NSMutableArray *nn1=[[NSMutableArray alloc]initWithArray:[nn componentsSeparatedByString:@""]];

NSMutableArray *nn3=[[NSMutableArray alloc]initWithArray:[[nn1 reverseObjectEnumerator]allObjects]];

NSString *nn2=[nn3 componentsJoinedByString:@""];

NSLog(@"bb%@",nn3);

NSLog(@"nnn%@",nn2);

NSMutableArray *d4=[[NSMutableArray alloc]initWithArray:[[d3 reverseObjectEnumerator]allObjects]];

//        NSArray *d4=[[d3 reverseObjectEnumerator]allObjects];

NSLog(@"倒序%@",d4);

//添加

NSLog(@"------添加----------");

[d3 addObject:@"ikj"];

// 指定的位置  [d3 insertObject:@"wser" atIndex:[i+1]

//删除

NSLog(@"-------删除---------");

[d3 removeObject:@"dda"];

//一次性显示

NSLog(@"-------一次性显示---------");

NSLog(@"%@\n\n",d3);

//循环显示

NSLog(@"-------循环显示---------");

for(NSString *f in d3)

{

NSLog(@"%@",f);

}

NSLog(@"\n\n");

//动态数组2

NSLog(@"------动态数组2----------");

NSMutableArray *p=[[NSMutableArray alloc]initWithCapacity:5];

[p addObject:@"5"];

[p addObject:@"8"];

[p addObject:@"2"];

[p addObject:@"1"];

[p addObject:@"9"];

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

{

NSLog(@"数值%@",p[i]);

}

NSArray *[email protected][@"5",@"8",@"2",@"4",@"9"];

//正序排列

NSLog(@"--------正序排列--------");

p1=[p1 sortedArrayUsingComparator:^NSComparisonResult(id p2, id p3)

{

NSComparisonResult p4=[p2 compare:p3];

return p4==NSOrderedDescending;

}];

NSLog(@"正序排序%@",p1);

//倒序排列

NSLog(@"------倒序排列----------");

p1=[p1 sortedArrayUsingComparator:^NSComparisonResult(id p2, id p3)

{

NSComparisonResult p4=[p2 compare:p3];

return p4==NSOrderedAscending;

}];

NSLog(@"倒序排列%@",p1);

//取值

NSLog(@"-------取值---------");

NSString *s = [p objectAtIndex:1];

NSLog(@"%@\n\n",s);

//排序

NSLog(@"-------排序---------");

NSMutableArray *d7=[[NSMutableArray alloc]initWithObjects:@"2",@"7",@"5",@"8",nil];

NSArray *d8=[d7 sortedArrayUsingSelector:@selector(compare:)];

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

{

NSLog(@"%@",d8[i]);

}

//

for(NSString *d9 in d8)

{

NSLog(@"%@",d9);

}

NSLog(@"\n\n");

NSArray *d9=[[d7 reverseObjectEnumerator]allObjects];

for(NSString *d10 in d9)

{

NSLog(@"%@",d10);

}

NSLog(@"\n\n");

/////不可变字典

NSLog(@"-------不可变字典---------");

//前值 后钥匙

NSLog(@"------前值 后钥匙----------");

NSDictionary *h=[NSDictionary dictionaryWithObjectsAndKeys:@"kk",@"1",@"pp",@"2",@"oi",@"3",nil];

//后值 前钥匙

NSLog(@"-------后值 前钥匙---------");

NSDictionary *[email protected]{@"1":@"ff",@"2":@"dd",@"3":@"yy"};

//选择钥匙对应值

NSLog(@"-------选择钥匙对应值---------");

NSString *i1=[h objectForKey:@"2"];

NSLog(@"选择钥匙对应的值%@",i1);

NSLog(@"\n\n");

//选择所有钥匙

NSLog(@"-------选择所有钥匙---------");

NSEnumerator *i2=[h keyEnumerator];

for(NSString *i3 in i2)

{

NSLog(@"钥匙%@",i3);

}

NSLog(@"\n\n");

//选择所有的值

NSLog(@"------选择所有的值----------");

NSEnumerator*i4=[h objectEnumerator];

for(NSString *i5 in i4)

{

NSLog(@"值%@",i5);

}

NSLog(@"\n\n");

//输出所有对应的值

NSLog(@"-------输出所有对应的值--------");

for(NSString *i6 in h)

{

NSLog(@"钥匙%@对应的值为%@",i6,[h objectForKey:i6]);

}

NSLog(@"\n\n");

//转为数组

NSLog(@"------转为数组----------");

NSArray *h2=[h allValues];

NSLog(@"数组为%@",h2);

NSArray *h3=[h1 allValues];

NSLog(@"数组为%@",h3);

//正序排序

NSLog(@"-------正序排序---------");

h2=[h2 sortedArrayUsingComparator:^NSComparisonResult(id h4, id h5)

{

NSComparisonResult h6=[h4 compare:h5];

return h6==NSOrderedDescending;

}];

NSLog(@"正序排序%@",h2);

/////可变字典

NSLog(@"-------可变字典---------");

NSDictionary *j=[NSDictionary dictionaryWithObjectsAndKeys:@"u23u",@"1",@"fre",@"2",@"eeoi",@"3",nil];

NSMutableDictionary *j1=[NSMutableDictionary dictionaryWithDictionary:j];

//增加

NSLog(@"---------增加-------");

[j1 setObject:@"hsja" forKey:@"4"];

for(NSString *k in j1)

{

NSLog(@"增加后%@,%@",k,[j1 objectForKey:k]);

}

NSLog(@"\n\n");

//删除

NSLog(@"-------删除---------");

[j1 removeObjectForKey:@"1"];

for(NSString *k in j1)

{

NSLog(@"删除后%@,%@",k,[j1 objectForKey:k]);

}

//字典排序1升序 2降序

NSLog(@"-------字典排序1升序 2降序---------");

NSMutableDictionary *dd1=[NSMutableDictionary dictionaryWithDictionary:@{@"name":@"赵本山",@"age":@"67",@"sex":@"nan"}];

NSMutableDictionary *dd6=[NSMutableDictionary dictionaryWithDictionary:@{@"name":@"张国荣",@"age":@"39",@"sex":@"nan"}];

NSMutableDictionary *dd7=[NSMutableDictionary dictionaryWithDictionary:@{@"name":@"刘晓庆",@"age":@"25",@"sex":@"wu"}];

NSMutableArray *ff=[[NSMutableArray alloc]initWithArray:@[dd1,dd6,dd7]];

//        NSMutableArray *ff=[[NSMutableArray alloc]initWithArray:@[@{@"name":@"liu",@"age":@"67",@"sex":@"nan"},

//                             @{@"name":@"zhang",@"age":@"79",@"sex":@"nan"},

//                             @{@"name":@"ahou",@"age":@"56",@"sex":@"wu"}

//                             ]];

NSSortDescriptor *ff1=[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:0];

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

{

if([ff[i][@"sex"] compare:@"wu"]==0)

{

ff[i][@"sex"][email protected]"ff";ff[i][@"name" ][email protected]"范冰冰";

//                [ff[i] setObject:@"shhg" forKey:@"sex"];

}

}

NSArray *pp=[ff sortedArrayUsingDescriptors:[NSArray arrayWithObjects:ff1, nil]];

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

{

NSMutableDictionary *ff2=[NSMutableDictionary dictionaryWithDictionary:pp[i]];

NSLog(@"%@  %@  %@",ff2[@"name" ],ff2[@"age"] ,ff2[@"sex"]);

}

NSLog(@"\n-------遍历------\n");

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

{

NSMutableDictionary *ff2=[NSMutableDictionary dictionaryWithDictionary:pp[i]];

[ff2 enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {

NSLog(@"%@ %@", key, obj);

}];

}

////集合

NSLog(@"\n\n---------集合-------");

NSSet *k1=[NSSet setWithObjects:@"1",@"2",nil];

for(NSString *k2 in k1)

{

NSLog(@"%@",k2);

}

if([k1 containsObject:@"1"]==YES)

{

NSLog(@"chenggong");

}

////可变集合

NSLog(@"-------可变集合---------");

NSMutableSet *k3=[NSMutableSet setWithSet:k1];

//添加

NSLog(@"-------添加---------");

[k3 addObject:@"5"];

NSLog(@"增加%@",k3);

//删除

NSLog(@"-------删除---------");

[k3 removeObject:@"2"];

NSLog(@"删除%@",k3);

//交集

NSLog(@"--------交集--------");

[k1 intersectsSet:k3];

NSLog(@"交集%@",k3);

//并集

NSLog(@"--------并集--------");

[k3 unionSet:k1];

NSLog(@"并集%@",k3);

//求合集指定的值的个数

NSLog(@"-------求合集指定的值的个数---------");

NSCountedSet *k4=[NSCountedSet setWithSet:k3];

[k4 addObject:@"3"];

[k4 addObject:@"5"];

[k4 addObject:@"7"];

NSLog(@"%ld",[k4 countForObject:@"2"]);

//字典转数组

NSArray *[email protected][

@{@"name":@"tom",@"age":@"54",@"id":@"1001"},

@{@"name":@"ggf",@"age":@"22",@"id":@"1002"},

@{@"name":@"rew",@"age":@"65",@"id":@"1003"},

];

NSMutableArray *bb1=[[NSMutableArray alloc]initWithObjects:bb, nil];

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

{

NSDictionary *aa=bb1[i];

NSLog(@"%@",aa);

}

//字符串变成数组

NSLog(@"---------字符串变成数组-------");

NSString *[email protected]"dsa,iua,gtrf,ouui.yri";

NSArray *j6=[j5 componentsSeparatedByString:@","];

NSLog(@"数组%@",j6);

for(NSString *f11 in j6)

{

NSLog(@"数值%@",f11);

}

NSLog(@"\n\n");

//数组变成字符串

NSLog(@"--------数组变成字符串--------");

NSString *j7=[j6 componentsJoinedByString:@","];

NSLog(@"字符串%@",j7);

//排序

//自定义数组

//        //123-456-789找到并且删除变成123456789   并且替换成" ";

//        //将字符串159和39,变成十进制(int型),相差,用字符串输出

//        NSMutableString *c11=[[NSMutableString alloc]init];

//        c11=[NSMutableString stringWithFormat:@"123-456-789"];

//        NSRange sub1=[c11 rangeOfString:@"-"];

//        while (sub1.location!=NSNotFound)

//        {

//            [c11 deleteCharactersInRange:sub1];

//            sub1=[c11 rangeOfString:@"-"];

//        }

//        NSLog(@"变成后%@\n\n",c11);

//

//        NSString *[email protected]"159",*[email protected]"39";

//        int a3=[a2 intValue];

//        int b3=[b2 intValue];

//        int b4=a3-b3;

//        NSString *b5=[NSString stringWithFormat:@"%d",b4];

//        NSLog(@"%@\n\n",b5);

//

//        NSString *[email protected]"wo yao chi fan";

//        NSLog(@"请找出chi并显示长度\n\n");

//        NSRange sub3=[p rangeOfString:@"chi"];

//        if(sub3.location!=NSNotFound)

//        {

//            NSLog(@"chi以找到");

//        }

////        unsigned long l=[p length];

//        NSLog(@"chi的长度为%ld",sub3.location);

}

return 0;

}

时间: 2024-08-29 16:41:34

oc程序代码的相关文章

【Objective-C】04-第一个OC程序解析

说明:这个Objective-C专题,是学习iOS开发的前奏,也为了让有面向对象语言开发经验的程序员,能够快速上手Objective-C.如果你还没有编程经验,或者对Objective-C.iOS开发不感兴趣,请忽略.学习本专题之前,建议先学习C语言专题. 在上一讲,已经新建了第一个OC程序,这讲就来解析它内部的代码. 查看项目结构可以发现这个程序中只有一个源文件:main.m 打开main.m文件,代码如下 1 #import <Foundation/Foundation.h> 2 3 in

黑马程序员----第一个OC程序

一.#import 1.#import的作用: 跟#include的作用一样,拷贝文件 可以自动防止文件的内容被重复拷贝 2.#import <Foundation/NSObjCRuntime.h> NSObjCRuntime.h文件中有SLog函数的声明 二.Foundation框架头文件的路径 1. 右击Xcode.app --> 显示包内容   2. Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer

黑马程序员 01-第一个OC程序

———Java培训.Android培训.iOS培训..Net培训.期待与您交流! ——— 1.代码编写 跟C语言一样,OC程序的入口是main函数,只不过是写在OC的源文件(.m文件)中. 比如这里的第一个OC main.m文件中(文件名可以是中文): 1 #include <stdio.h> 2 // OC程序的入口:main函数 3 // OC程序完全兼容C语言 4 int main(int argc, const char * argv[]) 5 { 6     printf("

简单的OC程序

Ø     Foundation框架头文件的路径 1> 右击Xcode.app --> 显示包内容 2> Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.0.sdk/System/Library/Frameworks/Foundation.framework 主头文件 1> 主头文件:最主要的头文件,名字一般跟框架名称一样,包含了框架中的所有其他头文件 2> F

黑马程序员 03-第二个OC程序

———Java培训.Android培训.iOS培训..Net培训.期待与您交流! ——— 1.代码编写 以下是第二个OC程序: 1 #import <Foundation/Foundation.h> 2 3 int main() 4 { 5     //NSLog输出内容会自动换行 6 //NSL3个字母都是大写 7 //@和""之间不能有空格 8     NSLog(@"第2个OC程序!!!"); 9      10     return 0; 11

黑马程序员——简单的OC程序

知识点 1.#import的用途: 1> 跟#include一样,拷贝文件的内容 2> 可以自动防止文件的内容被重复拷贝 2.#import <Foundation/NSObjCRuntime.h> NSObjCRuntime.h中有NSLog函数的声明 3.Foundation框架头文件的路径 1> 右击Xcode.app --> 显示包内容 2> Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/

2、第一个OC程序

方法一:在Mac终端下运行第一个OC程序 1> 先装好Xcode6.1.1 2> 打开终端工具 3>使用unix命令执行以下操作: 附上代码(003.m): #import <Foundation/Foundation.h> int main () { //NSLog执行之后会自动换行 NSLog (@"\n第1个OC程序!!!"); return 0; } /* 1.#import的用途:  1> 跟#include一样,拷贝文件的内容  3>

第一个OC程序 HelloWorld

打开Xcode,新建一个Xcode项目,在OS X下面选择Application,再选择Command Line Tool,点击next,再下一个页面输入项目名称,点击next就完成了.此时,我们可以看到一个main.m的文件,其中有如下代码: #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... NSLog(@&

【黑马程序员】————简单的OC程序

一.  OC简介 C语言的基础上,增加了一层最小的面向对象语法 完全兼容C语言 可以在OC代码中混入C语言代码,甚至是C++代码 可以使用OC开发Mac OS X平台和iOS平台的应用程序 二.  OC语法预览 1.      关键字 基本上所有关键字都是以@开头 下面列举一些常见的关键字,看一眼就行了,不用去翻资料查找它们的含义 @interface.@implementation.@end @public.@protected.@private.@selector @try.@catch.@