OC入门简单知识点,其实也没那么难(真的吗?走着瞧!)
- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
// 程序启动成功就进入此方法
printf(“didFinishlaunchingWithOptions\n”);
printf(“didFinishlaunchingWithOptions\n”);
// OC的控制台打印方法,OC是乔布斯NextStep的,所以都有NS前缀
// c语言的字符串是:”“,OC为了区分,在前面加一个@:@””
// OC打印,自带换行符,带有打印的时间。
NSLog(@”didFinishlaunchingWithOptions”);
NSLog(@”a is %d, b is %d, c is %f”,12, 23, 56.0);
// c语言常用基础类型:int long
// OC 使用NSInteger 代替
// 调用c语言函数
function();
// OC语言的方法调用,需要写在[]中,形式为[A b];A为b的所有者
[self function];
[self functionJiOu:121511416];
[self functionqiuHe:1 anotherNum:100];
[self forNum:122222 anotherNum:78 anotherNum:45 anotherNum:12];
return YES;
}
//返回最大值,三目运算
- (NSInteger)getMaxNum:(NSInteger)Num
anotherNum:(NSInteger)anotherNum{
return Num > anotherNum ? Num : anotherNum;
}
- (NSInteger)getMinNum:(NSInteger)Num
anotherNum:(NSInteger)anotherNum{
return Num < anotherNum ? Num : anotherNum;
}
//求a到b中,能被c,d整除的数量
- (void) forNum:(NSInteger)a
anotherNum:(NSInteger)b
anotherNum:(NSInteger)c
anotherNum:(NSInteger)d{
NSInteger sum;
NSInteger maxNum = [self getMaxNum:a anotherNum:b];
NSInteger minNum = [self getMinNum:a anotherNum:b];
for (NSInteger i = minNum; i <= maxNum; i ++) {
if (i % c == 0 && i % d == 0) {
sum ++;
}
}
NSLog(@”sum等于%ld”,(long)sum);
}
// 求a到b之间全部素数个数
- (void) getSuShu:(NSInteger)a
anotherNum:(NSInteger)b{
NSInteger sum = 0;
NSInteger MaxNum = [self getMaxNum:a anotherNum:b];
NSInteger MinNum = [self getMinNum:a anotherNum:b];
for (NSInteger f = MinNum; f <= MaxNum; f ++) {
for (NSInteger s = 2; s < MaxNum; s ++) {
if (f % s != 0) {
sum ++;
}
}
}
NSLog(@”sum = %ld”,sum);
}
//判断A到B之间所有数字的和
//多参数之间,使用(空格 + :);间隔,前面可以加描述,使人读代码更方便易懂
- (void)functionqiuHe:(NSInteger)number1
anotherNum:(NSInteger)number2{
NSInteger sum = 0;
NSInteger maxNum = [self getMaxNum:number1 anotherNum:number2];
NSInteger minNum = [self getMinNum:number1 anotherNum:number1];
for (NSInteger i = minNum; i <= maxNum; i ++) {
sum += i;
}
NSLog(@”sum is %ld”,(long)sum);
}
//判断一个数的奇偶
- (void)functionJiOu:(int)number{
// 如果是奇数,打印。。。,是偶数,打印。。。
// 使用%@打印字符串
NSLog(@”%d是%@”, number, number % 2 == 0 ? @”偶数” : @”奇数”);//三目运算
}
- (void)function
{
NSLog(@”OC 函数”);
}
void function(){
printf(“c函数\n”);
}