Objective-C入门02:一些简单的基本操作
2015-09-14 16:28
344 查看
Objective-C入门02:一些简单的基本操作
程序启动成功后,就会进入这个方法。- (BOOL)application:(UIApplication
*)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
c语言的字符串是"",oc为了区分,前面加一个@
oc的打印, 自带换行符, 带有打印时间。
c语言常用的基础类型 int long
oc语言中 ,使用
NSInteger 代替 int long
NSLog(@"asdflkasdfklsadf" );
NSLog(@"a is %d,b is %d ,c is %f",12, 33, 28.11 ) ;
c语言的调用
function ( ) ;
oc语言的调用
[selffunction ] ;
判断奇数偶数调用
[selffunctionJiOu : 123 ] ;
判断从A到B之间所有数字的和调用
[selfqiuHe : 100 : 1 ] ;
数字A到B,能同时被数字c,d整除的数字的数量
[selfshu1 : 1 shu2
: 100 shu3 : 10 shu4 : 5 ] ;
returnYES ;
}
c语言的方法
void function ()
{
printf("我是c语言函数\n"
) ;
}
oc语言的方法
-(void )function
{
NSLog(@"我是oc语言方法"
) ;
}
判断从A到B之间所有数字的和;
多参数之间,使用(空格+:):号隔开;
多参数 :前面可以加描述,让他人读代码跟容易。
- (void ) qiuHe : (NSInteger
)number1 : (NSInteger )number2
{
NSInteger maxNum = [selfgetMaxNumWithNum
: number1anotherNum : number2 ] ;
NSInteger minNum = [selfgetMaxNumWithNum
: number1anotherNum : number2 ] ;
NSInteger sum ;
for (NSInteger i = minNum
; i <= maxNum ; i++ )
{
sum+= i ;
}
NSLog(@"sum is %ld" , sum )
;
}
数字A到B,能同时被数字c,d整除的数字的数量
- (void ) shu1 : (NSInteger )number1
shu2 : (NSInteger )number2
shu3 : (NSInteger )number3
shu4 : (NSInteger )number4
{
NSInteger sum = 0 ;
NSInteger maxNum = [selfgetMaxNumWithNum
: number1anotherNum : number2 ] ;
NSInteger minNum = [selfgetMinNumWithNum
: number1anoutherNum : number2 ] ;
for (NSInteger i = minNum
; i <= maxNum ; i++ )
{
if(( (i%number3)==0)&&((i%number4)==0))
sum++;
}
NSLog(@"%ld到%ld的数中能被%ld和%ld整除的数量为%ld",number1,number2,number3,number4,sum);
}
方法:返回两个数字中,较大的数
- (NSInteger )getMaxNumWithNum : (NSInteger
)num
anotherNum : (NSInteger )anotherNum
{
return num > anotherNum ? num : anotherNum ;
}
方法:返回两个数字中,较小的数
- (NSInteger )getMinNumWithNum : (NSInteger
)num
anoutherNum : (NSInteger )anoutherNum
{
return num > anoutherNum ? anoutherNum : num ;
}
把三个数按小到大输出
- (void )getMinGoMax : (NSInteger
) num1
getMinGoMax : (NSInteger ) num2
getMinGoMax : (NSInteger ) num3
{
num1 = [selfgetMinNumWithNum :
num1anoutherNum : num2 ] ;
num2 = [self getMaxNumWithNum :
num1anotherNum : num2 ] ;
}
判断奇数偶数
-(void )functionJiOu : (int
a3cc
) number
{
使用%@打印字符串
如果是奇数,打印出number是奇数,否则打印出number是偶数
NSLog(@" %d是
%@ " , number , number%2 == 0 ? @"偶数"
:@"奇数");
}
相关文章推荐
- Objective-C的内省(Introspection)用法小结
- Objective-C中常用的结构体NSRange,NSPoint,NSSize(CGSize),NSRect实例分析
- Objective-C中NSLog输出格式大全
- xcode 4 制作静态库图文详解
- Swift调用Objective-C编写的API实例
- Swift、Objective-C、Cocoa混合编程设置指南
- Objective-c代码如何移植为Swift代码 Objective-c代码转移到Swift过程介绍
- 解决 Xcode 6-Beta2 智能提示bug
- Swift调用Objective-C代码
- XCode编译速度慢的处理方法
- 以实例讲解Objective-C中的KVO与KVC机制
- objective-c中生成随机数的方法
- 在Swift中使用Objective-C编写类、继承Objective-C类
- mac git xcrun error active developer path 错误
- Swift能代替Objective-C吗?
- Objective-C 消息传递机制详解
- 在一个项目中同时使用Swift和Objective-C代码混合编程的方法
- Xcode使用教程详细讲解(全)
- vm安装mac os x 10.7 lion