본문 바로가기

개발/Objective-C

[Objective-C] Swift와 비교 (2) - 변수 생성 및 ViewController

반응형

변수

1. Swift

import UIKit

var view = UIView(frame: .zero)
let image = UIImage(named: "sample.png")
let imageView = UIImageView(image: image)

 

2. Objective - C

#import <UIKit/UIKit.h>

UIView *view = [UIView init];
UIImage *image = [UIImage imageNamed:@"sample.png"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectZero];
imageView.image = image;

 

ViewController

1. Swift

import UIKit

final class TestViewController: UIViewController {
    
    // MARK: - UI Components
  
    private let menuTitlelabel : UILabel = {
        let label = UILabel(frame: .zero)
        label.font = UIFont.boldSystemFont(ofSize: 20)
        label.text = "메뉴"
        return label
    }()
    
    private let menuCollectionView : UICollectionView = {
        let cv = UICollectionView(frame: .zero, collectionViewLayout: .init())
        cv.showsVerticalScrollIndicator = false
        
        cv.register(TestCollectionViewCell.self, forCellWithReuseIdentifier: TestCollectionViewCell.cellID)
        return cv
    }()
    
    // MARK: - Property
    
    let menu : [String]
    
    // MARK: - initialize
    
    init(menu : [String]) {
        self.menu = menu
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("TestViewController - init(coder:) has not been implemented")
    }
    
    // MARK: - LifeCycle

    override func viewDidLoad() {
        super.viewDidLoad()
        setUpSubViews()
    }
    
    // MARK: - Methods
    
    private func setUpSubViews() {
        let safe = view.safeAreaLayoutGuide
        
        view.addSubview(menuTitlelabel)
        view.addSubview(menuCollectionView)
        
        menuTitlelabel.translatesAutoresizingMaskIntoConstraints = false
        menuTitlelabel.topAnchor.constraint(equalTo: safe.topAnchor, constant: 20).isActive = true
        menuTitlelabel.centerXAnchor.constraint(equalTo: safe.centerXAnchor).isActive = true
        
        menuCollectionView.translatesAutoresizingMaskIntoConstraints = false
        menuCollectionView.topAnchor.constraint(equalTo: menuTitlelabel.bottomAnchor, constant: 50).isActive = true
        menuCollectionView.bottomAnchor.constraint(equalTo: safe.bottomAnchor).isActive = true
        menuCollectionView.leftAnchor.constraint(equalTo: safe.leftAnchor).isActive = true
        menuCollectionView.rightAnchor.constraint(equalTo: safe.rightAnchor).isActive = true

        menuCollectionView.delegate = self
        menuCollectionView.dataSource = self
    }
}

 

2. Objective - C

/* Test2ViewController.h */
#import <UIKit/UIKit.h>

@interface Test2ViewController : UIViewController
    #pragma mark - Property
    @property(nonatomic, weak) NSArray<NSString *> *menu;

@end


/* Test2ViewController.m */
#import "Test2ViewController.h"
#import "Test2CollectionViewCell.h"
#import <UIKit/UIKit.h>

// 익명 카테고리 - Private
@interface Test2ViewController () 
    #pragma mark - Private UI Components
    @property(nonatomic, strong) UILabel* menuTitleLabel;
    @property(nonatomic, strong) UICollectionView* menuCollectionView;
@end

@implementation Test2ViewController

    #pragma mark - initialize
    - (id)init: (NSArray<NSString *> *)menu {
        self = [super init];
        self.menu = menu;
        return self;
    }

    #pragma mark - LifeCycle
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self setUpSubViews];
    }

    #pragma mark - Methods
    - (void) setUpSubViews {
        const UILayoutGuide *safe = self.view.safeAreaLayoutGuide;
        
        _menuTitleLabel.font = [UIFont boldSystemFontOfSize:20];
        _menuTitleLabel.text = @"메뉴";
        
        _menuCollectionView.showsVerticalScrollIndicator = false;
        [_menuCollectionView registerClass:[Test2CollectionViewCell class] forCellWithReuseIdentifier: @"Test2CollectionViewCell"];
        
        [self.view addSubview: _menuTitleLabel];
        [self.view addSubview: _menuCollectionView];
        
        _menuTitleLabel.translatesAutoresizingMaskIntoConstraints = false;
        [_menuTitleLabel.topAnchor constraintEqualToAnchor: safe.topAnchor constant: 20].active = true;
        [_menuTitleLabel.centerXAnchor constraintEqualToAnchor: safe.centerXAnchor].active = true;
        
        _menuCollectionView.translatesAutoresizingMaskIntoConstraints = false;
        [_menuCollectionView.topAnchor constraintEqualToAnchor: safe.topAnchor constant:50].active = true;
        [_menuCollectionView.bottomAnchor constraintEqualToAnchor: safe.bottomAnchor].active = true;
        [_menuCollectionView.leftAnchor constraintEqualToAnchor: safe.leftAnchor].active = true;
        [_menuCollectionView.rightAnchor constraintEqualToAnchor: safe.rightAnchor].active = true;
    }

@end

 

Extension

1. Swift

extension TestViewController : UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return menu.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TestCollectionViewCell.cellID, for: indexPath) as! TestCollectionViewCell
        cell.bindViewModel(url: menu[indexPath.row])
        return cell
    }
}

 

2. Objective - C

/* Test2ViewController+More.h */
#import <UIKit/UIKit.h>
#import "Test2ViewController.h"

@interface Test2ViewController (More) <UICollectionViewDelegate, UICollectionViewDataSource>
    - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
@end


/* Test2ViewController+More.m */
#import "Test2ViewController+More.h"

@implementation Test2ViewController (More)

- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    UICollectionViewCell *collectionCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Test2CollectionViewCell" forIndexPath:indexPath];
    return collectionCell;
}

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.menu.count;
}

@end

 

Cell 

1. Swift

final class TestCollectionViewCell : UICollectionViewCell {
    
    // MARK: - UI Components
    
    private let foodImageView: UIImageView = {
        let iv = UIImageView(frame: .zero)
        iv.contentMode = .scaleAspectFit
        return iv
    }()
    
    // MARK: - Property
    
    static let cellID = "TestCollectionVieWCell"
    
    // MARK: - initialize
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpSubViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - Methods
    
    private func setUpSubViews() {
        let safe = contentView.safeAreaLayoutGuide
        
        contentView.addSubview(foodImageView)
        
        foodImageView.translatesAutoresizingMaskIntoConstraints = false
        foodImageView.topAnchor.constraint(equalTo: safe.topAnchor).isActive = true
        foodImageView.bottomAnchor.constraint(equalTo: safe.bottomAnchor).isActive = true
        foodImageView.leftAnchor.constraint(equalTo: safe.leftAnchor).isActive = true
        foodImageView.rightAnchor.constraint(equalTo: safe.rightAnchor).isActive = true
    }
    
    func bindViewModel(url: String) {
        guard let image = UIImage(named: url) else { return }
        
        DispatchQueue.main.async { [weak self] in
            self?.foodImageView.image = image
        }
    }
}

 

2. Objective - C

/* Test2CollectionViewCell.h */
#import <UIKit/UIKit.h>

@interface Test2CollectionViewCell : UICollectionViewCell
    #pragma mark - UI Components
    @property(nonatomic, strong) UIImageView* foodImageView;

    #pragma mark - Methods
    - (void) bindViewModel: (NSString*) url;
@end


/* Test2CollectionViewCell.m */
#import <UIKit/UIKit.h>
#import "Test2CollectionViewCell.h"

@interface Test2CollectionViewCell ()
    #pragma mark - private Methods
    - (void) setUpSubViews;
@end

@implementation Test2CollectionViewCell
    #pragma mark - Property
    const NSString* cellID = @"Test2CollectionViewCell";

    #pragma mark - initialize
    - (id)initWithFrame:(CGRect)frame {
        self = [super initWithFrame:frame];
        [self setUpSubViews];

        return self;
    }

    #pragma mark - Methods
    - (void) setUpSubViews {
        const UILayoutGuide *safe = self.contentView.safeAreaLayoutGuide;
        
        [self.contentView addSubview:_foodImageView];
        
        _foodImageView.translatesAutoresizingMaskIntoConstraints = false;
        
        [_foodImageView.topAnchor constraintEqualToAnchor: safe.topAnchor].active = true;
        [_foodImageView.bottomAnchor constraintEqualToAnchor: safe.bottomAnchor].active = true;
        [_foodImageView.leftAnchor constraintEqualToAnchor: safe.leftAnchor].active = true;
        [_foodImageView.rightAnchor constraintEqualToAnchor: safe.rightAnchor].active = true;
    }

    - (void) bindViewModel: (NSString *) url {
        if (url != nil) {
            __weak typeof(self) weakSelf = self;
            
            dispatch_async(dispatch_get_main_queue(), ^{
                weakSelf.foodImageView.image = [UIImage imageNamed:url];
            });
        }
    }
@end
반응형