OC入门
来源:互联网 发布:淘宝仿真猫咪 编辑:程序博客网 时间:2024/06/11 02:57
OC与C的区别
#import "AppDelegate.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 程序启动成功了,就会进入这个方法 printf("didFinishLaunchingWithOptions"); printf("didFinishLaunchingWithOptions\n"); // OC的控制台打印方法, 因为OC是乔布斯前公司 NextStep的, 所有都有NS前缀 // C语言的字符串 是 "", OC为了区分,前面加了一个@ // OC的打印 自带换行符, 带有打印的时间。 NSLog(@"didFinishLaunchingWithOptions"); NSLog(@"a is %d, b is %d, c is %f", 12, 33, 123.0); // C语言常用的基础类型, int long // OC语言中, 使用NSInteger 代替 int long; //int a; //long b; //NSInteger c; // 调用C语言函数 function(); // OC语言的方法调用,需要写在[]中,形式为[A b], A为b的所有者 [self function]; [self functionJiOu:123]; [self qiuHeWithNum:1 anotherNum:100]; NSLog(@"n is %ld",[self chu:1 anotherNum:10 anotherNum:2 anotherNum:5]); return YES;}
方法,返回两个数字中,较大的数
- (NSInteger)getMaxNumWithNum:(NSInteger)num anotherNum:(NSInteger)anotherNum{ return num > anotherNum ? num : anotherNum;}
方法,返回两个数字中,较小的数
- (NSInteger)getMinNumWithNum:(NSInteger)num anotherNum:(NSInteger)anotherNum{ return num < anotherNum ? num : anotherNum;}
求数字A 到 数字B 中 能同时被数字C,D整除的数字的数量
- (NSInteger)chu:(NSInteger)num1 anotherNum:(NSInteger)num2 anotherNum:(NSInteger)num3 anotherNum:(NSInteger)num4{ NSInteger n; NSInteger min = [self getMinNumWithNum:num1 anotherNum:num2]; NSInteger max = [self getMaxNumWithNum:num1 anotherNum:num2]; for (NSInteger i = min; i <= max; i ++) { if (!(i%num3)&&!(i%num4)) { n ++; } } return n;}
判断从A 到 B 之间所有数字的和
-多参数之间 使用(空格+:) :号间隔
-多参数 :前面可以加描述,让他人读代码更容易理解。
- (void)qiuHeWithNum:(NSInteger)number1 anotherNum:(NSInteger)number2{ NSInteger sum; NSInteger minNum = [self getMinNumWithNum:number2 anotherNum:number1]; NSInteger maxNum = [self getMaxNumWithNum:number1 anotherNum:number2]; for (NSInteger i = minNum; i <= maxNum; i ++) { sum += i; } NSLog(@"sum is %ld", (long)sum);}
判断一个数字是奇数 还是 偶数
- (void)functionJiOu:(NSInteger)number{ // 如果是奇数,打印出 number 是奇数, 否则打印出 number是偶数 // 使用 %@打印字符串 NSLog(@"%ld 是 %@", (long)number, number%2 == 0 ? @"偶数" : @"奇数" );}
OC语言的方法
- (void)function{ NSLog(@"我是OC语言方法");}
C语言的方法
void function(){ printf("我是C语言函数\n");}
输出结果
didFinishLaunchingWithOptionsdidFinishLaunchingWithOptions2015-09-30 20:11:06.304 OC入门[3553:293850] didFinishLaunchingWithOptions2015-09-30 20:11:06.359 OC入门[3553:293850] a is 12, b is 33, c is 123.000000我是C语言函数2015-09-30 20:11:06.360 OC入门[3553:293850] 我是OC语言方法2015-09-30 20:11:06.360 OC入门[3553:293850] 123 是 奇数2015-09-30 20:11:06.360 OC入门[3553:293850] sum is 50502015-09-30 20:11:06.360 OC入门[3553:293850] n is 1
0 0
- OC---oc基础入门
- OC入门
- OC入门
- OC-入门
- oc入门
- OC 入门语法大全
- OC基本语法入门
- OC入门笔记
- OC入门第一课
- oc语法快速入门
- OC语言入门
- OC 宏入门
- OC入门、面向对象语法
- [转载]UIKit Dynamics入门(OC)
- OC 入门实现登陆验证
- 一 :ios学习笔记 OC入门1
- 黑马程序员 OC------入门语法学习
- 【IOS】IOS快速入门之OC语法
- 随想录(cloud 网络库)
- 剑指offer—链表中倒数第k个结点
- 数据的交换输出
- 利用百度地图SDK进行开发时申请key和基本配置以及一个小问题
- AVPlayer自定义视频
- OC入门
- test9.12
- jQuery中的事件冒泡和解决方案
- dinic
- Java多维数组遍历
- 文章标题
- 应用spring mvc 在controller间跳转 重定向
- VirtualBox错误:Unable to load R3 module 解决方法
- JAVA中int、String的类型转换