judaco / Objective-C

0 stars 0 forks source link

Objective-C - Lesson 06 #5

Open judaco opened 7 years ago

judaco commented 7 years ago

Protocol HomeWork for Delegated (Listeners)

//
//  main.m
//  Protocol - HW
//
//  Created by hackeru on 29/03/2017.
//  Copyright © 2017 juda. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Button.h"
#import "Button2.h"

@interface MyDelegate : NSObject <ButtonDelegate>

@end

@implementation MyDelegate

-(void)click {
    NSLog(@"Button was clicked");
}

@end

@interface MyClass : NSObject
-(void) stam;
@end

@implementation MyClass

-(void)stam {
    NSLog(@"stam");
}

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
//        MyDelegate *myDelegate = [[MyDelegate alloc] init];
//        Button *button = [[Button alloc] init];
//        button.delegate = myDelegate;
//        [button simulateClick];

        Button2 *button2 = [[Button2 alloc] init];
        MyClass *myClass = [[MyClass alloc] init];
        [button2 setTarget:@"stam" onObject:myClass];
        [button2 simulateClick];
    }
    return 0;
}
//
//  Button.h
//  Protocol - HW
//
//  Created by hackeru on 29/03/2017.
//  Copyright © 2017 juda. All rights reserved.
//

#import <Foundation/Foundation.h>

@protocol ButtonDelegate <NSObject>

-(void) click;

@end

@interface Button : NSObject
@property id<ButtonDelegate> delegate;
-(void) simulateClick;
@end
//
//  Button.m
//  Protocol - HW
//
//  Created by hackeru on 29/03/2017.
//  Copyright © 2017 juda. All rights reserved.
//

#import "Button.h"

@implementation Button

@synthesize delegate;

-(void)simulateClick {
    if (delegate != nil) {
        if ([delegate respondsToSelector:NSSelectorFromString(@"click")]) {
            [delegate click];
        }
    }
}
@end
//
//  Button2.h
//  Protocol - HW
//
//  Created by hackeru on 29/03/2017.
//  Copyright © 2017 juda. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Button2 : NSObject
-(void) setTarget: (NSString*) t onObject: (id) o;
-(void) simulateClick;
@end
//
//  Button2.m
//  Protocol - HW
//
//  Created by hackeru on 29/03/2017.
//  Copyright © 2017 juda. All rights reserved.
//

#import "Button2.h"

@implementation Button2
{
    NSString * target;
    id obj;
}

-(void)simulateClick {
    if (obj != nil && target != nil) {
        if ([obj respondsToSelector:NSSelectorFromString(target)]) {
            [obj performSelector:NSSelectorFromString(target)];
        }
    }
}

-(void)setTarget:(NSString *)t onObject:(id)o {
    target = t;
    obj = o;
}

@end
judaco commented 7 years ago

Array in C

//
//  main.m
//  Lesson06 - Working with Arrays in C
//
//  Created by hackeru on 29/03/2017.
//  Copyright © 2017 juda. All rights reserved.
//

#import <Foundation/Foundation.h>

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

@implementation Dog

-(void)bark {
    NSLog(@"waf... waf....");
}

@end

int sumOfElements (int * nums[], int length) {
    int sum = 0;
    for (int i = 0; i < length; i++) {
        int temp = nums[i];
        sum += temp;
        //sum += (int) nums[i];//casting of the array of nums to int, in order to not calculate the sum in bytes (instead).
        //other way is to chane the (int * nums) and - sum += *(nums+i*2) - the compiler is attending to the int as "short", +2, +2
    }
    return sum;
}

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

int main(int argc, const char * argv[]) {
    @autoreleasepool {
//        int *nums[10];
//        for (int i = 0; i < 10; i++) {
//            nums[i] = i+1;
//        }
//        NSLog(@"sum = %i", sumOfElements(nums, 10));
//        for (int i = 0; i < 10; i++) {
//            NSLog(@"%i", nums[i]);
//        }

//        Dog *dogs[10];
//        for (int i = 0; i < 10; i++) {
//            dogs [i] = [[Dog alloc] init];
//        }
//        for (int i =0; i < 11; i++) {
//            Dog *d = dogs[i];
//            if (d == nil)
//                continue;
//            [dogs[i] bark];
//            NSLog(@"%i", i);
//        }

//        int *matrix[2][5];
//        matrix[0][0] = 100;
//        NSLog(@"%i", matrix[0][0]);

        int * n;
        n = functionThatReturnArray();
        NSLog(@"%i", *(n+2));//go to the address in the memory and see what exists there

    }
    return 0;
}
judaco commented 7 years ago
//
//  main.m
//  Pointers in C
//
//  Created by hackeru on 29/03/2017.
//  Copyright © 2017 juda. All rights reserved.
//

#import <Foundation/Foundation.h>

void changeSomeInteger (int *x){
    *x = *x + 1;//change the position of x by reference
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        int *x = 4;//x in the memory, not the value of x
        int *y = &x;
        //y = 101;//error
        *y = 101;
        NSLog(@"%i", *y);//*y will give me the address of the position of x and the value of x
        NSLog(@"%i", &y);//&y the address in the memory where y exists
        NSLog(@"%i", &x);//&x - the address in the memory where x exists
        NSLog(@"%i", x);//will print the value of y
        //conclusion - I can change primitive types (int) by reference

        int a = 3;
        changeSomeInteger(&a);
        NSLog(@"a=%i", a);//will give me a = 4

        //&x - the address in the memory where x exists * y will give me the address on x in the memory * but *y will give me the address of the position of x and the value of x
    }
    return 0;
}