面试题答案
一键面试1. 搭建混编环境
在Xcode项目中,创建一个桥接头文件(YourProjectName - Bridging - Header.h
),用于在Objective - C和Swift之间进行相互调用。如果是从Objective - C项目引入Swift,Xcode会自动提示创建桥接头文件。在桥接头文件中,导入需要在Swift中使用的Objective - C头文件,例如:
#import "ObjectiveCClass.h"
如果要在Objective - C中使用Swift代码,需要导入生成的Swift头文件,格式为YourProjectName - Swift.h
,在需要使用Swift类的Objective - C文件中导入:
#import "YourProjectName - Swift.h"
2. 设计模式融合
- Model层:
- 数据模型定义:无论是Objective - C还是Swift,都可以定义数据模型类。在Swift中可以使用结构体或类来定义,例如:
struct User {
var name: String
var age: Int
}
在Objective - C中定义:
@interface User : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger age;
@end
- 数据持久化:可以使用Core Data(它在Objective - C和Swift中都有很好的支持)进行数据持久化。例如,在Swift中获取Core Data的
NSManagedObjectContext
:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
在Objective - C中:
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
NSManagedObjectContext *context = appDelegate.persistentContainer.viewContext;
- View层:
- 视图创建:在Objective - C中可以使用
UIView
及其子类创建视图,在Swift中同样可以。例如,创建一个简单的UIButton
: Swift代码:
- 视图创建:在Objective - C中可以使用
let button = UIButton(type:.system)
button.setTitle("Click me", for:.normal)
button.addTarget(self, action: #selector(buttonTapped), for:.touchUpInside)
self.view.addSubview(button)
Objective - C代码:
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@"Click me" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonTapped) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
- 布局管理:可以使用Auto Layout(在Objective - C和Swift中使用方式类似),例如在Swift中:
button.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
button.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
button.centerYAnchor.constraint(equalTo: self.view.centerYAnchor)
])
Objective - C中:
button.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *centerXConstraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
NSLayoutConstraint *centerYConstraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0];
[self.view addConstraints:@[centerXConstraint, centerYConstraint]];
- ViewModel层:
- 共享逻辑:可以将一些业务逻辑封装在ViewModel类中,无论是Objective - C还是Swift实现的ViewModel,都可以被视图控制器调用。例如,在Swift中定义一个简单的ViewModel:
class UserViewModel {
var user: User?
func fetchUser() {
// 模拟网络请求获取用户数据
let newUser = User(name: "John", age: 30)
user = newUser
}
}
在Objective - C中:
@interface UserViewModel : NSObject
@property (nonatomic, strong) User *user;
- (void)fetchUser;
@end
@implementation UserViewModel
- (void)fetchUser {
User *newUser = [[User alloc] init];
newUser.name = @"John";
newUser.age = 30;
self.user = newUser;
}
@end
3. 关键模块间交互示例
假设我们有一个视图控制器ViewController
,需要与ViewModel交互获取用户数据并显示。
- Swift视图控制器调用Objective - C的ViewModel:
在桥接头文件导入Objective - C的ViewModel头文件
#import "UserViewModel.h"
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let viewModel = UserViewModel()
viewModel.fetchUser()
if let user = viewModel.user {
print("User name: \(user.name), age: \(user.age)")
}
}
}
- Objective - C视图控制器调用Swift的ViewModel:
导入Swift的ViewModel头文件
#import "YourProjectName - Swift.h"
#import "ViewController.h"
#import "YourProjectName - Swift.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UserViewModel *viewModel = [[UserViewModel alloc] init];
[viewModel fetchUser];
User *user = viewModel.user;
NSLog(@"User name: %@, age: %ld", user.name, (long)user.age);
}
@end
通过以上方式,可以在基于MVVM架构的iOS项目中有效地融合Objective - C和Swift的设计模式,确保代码的可维护性、扩展性和一致性。