judaco / Objective-C

0 stars 0 forks source link

Objective-C - Lesson 07 #6

Open judaco opened 7 years ago

judaco commented 7 years ago

Blocks

#import <Foundation/Foundation.h>

//void myFunc(){
//    NSLog(@"in myFunc()");
//}

@interface Dog : NSObject
@property int age;
-(void)bark;
@end

@implementation Dog
@synthesize age;
-(void)bark{
    NSLog(@"waf waf...");
}

@end

void shalev(void (^func)(void)){
    func();

}

void swap(int * x, int * y){
    int temp = *x;
    *x = *y;
    *y = temp;
}

void bubbleSort(int nums[], int n, int(^comparator)(int,int)){
    BOOL isSorted = NO;
    int upTo = n-1;
    while(!isSorted){
        isSorted = YES;
        for (int i=0; i<upTo; i++) {
            if(comparator(nums[i],nums[i+1]) == 1){
                isSorted = NO;
                swap(&nums[i], &nums[i+1]);
            }
        }
        upTo--;
    }
}

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

//        void (^myFunc)(void) = ^(void){
//            NSLog(@"in block");
//        };
//        
//        myFunc();
//        shalev(^{
//            NSLog(@"in anonymous function"); 
//        });
        //shalev(myFunc);
//        int (^myFunc2)(int,int) = ^(int x, int y){
//            NSLog(@"in block");
//            return x*2 + y;
//        };
//        
//        NSLog(@"%i", myFunc2(3,1));

        int nums[5] = {7,2,3,8,1};
        bubbleSort(nums, 5, ^int(int x, int y) {
            if(x > y)
                return 1;
            else if(x == y)
                return 0;
            else
                return -1;
        });
        for (int i=0; i<5; i++) {
            NSLog(@"%i",nums[i]);
        }

        int (^myFunc3) (id,id) = ^(Dog * d1, Dog * d2){

            if(d1.age > d2.age)
                return 1;
            else if(d1.age == d2.age)
                return 0;
            else
                return -1;

        };

    }
    return 0;
}
judaco commented 7 years ago

Arrays in C - Including Bubble Sort

#import <Foundation/Foundation.h>

void handleArray(int num[], int n){
    for (int i=0; i < n; i++) {
        NSLog(@"element = %i", num[i]);
    }
}

int * returnArray(){
    int arr[3];
    arr[0] = 5;
    arr[1] = 6;
    arr[2] = 7;
    return &arr;
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        /*
        int x = 5;
        int * y = &x;
        NSLog(@"%i", x);
        NSLog(@"%i", y);
        NSLog(@"%i", *y);
        NSLog(@"%i", &x);
        NSLog(@"%i", &y);
        */

        int nums[10];
        NSLog(@"size of nums=%i", sizeof(nums));

        for (int i=0; i<10; i++) {
            nums[i] = i+1;
            //*(nums+i) = i + 1;
        }

        handleArray(nums, 10);

        int sizeOfInt = sizeof(int);
        NSLog(@"sizeOfInt=%i", sizeOfInt);

        int * arr = returnArray();
        for (int i=0; i<3; i++) {
            NSLog(@"arr[i]=%i",*(arr+i));
        }

        NSMutableArray<NSNumber *> * numbers = [[NSMutableArray<NSNumber*> alloc] init];
        [numbers addObject:[NSNumber numberWithInt:5]];
        [numbers addObject:[NSNumber numberWithInt:6]];
        [numbers addObject:[NSNumber numberWithInt:7]];
        for (int i=0; i<numbers.count; i++) {
            NSNumber * number = numbers[i];
            int x = number.intValue;
            NSLog(@"%i", x);
        }

    }
    return 0;
}