如何将我的 obj C 代码转换为 swift 代码

法拉兹·A·汗

我开始只使用 Swift 在 iOS 上工作,并且对目标 C 一无所知,任何人都可以帮助我将用目标 C 编写的代码转换为 Swift 4。

UIImageView *iconView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 50)];
iconView.image = [UIImage imageNamed:@"my-icon-image"];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 35, 50)];
label.text = @"test";
[iconView addSubview:label];

UIGraphicsBeginImageContextWithOptions(label.bounds.size, NO, [[UIScreen mainScreen] scale]);
[iconView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *icon = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(lat, lng);
marker.title = @"my title";
marker.icon = icon;

marker.map = mapView;

另外,如果有人知道免费翻译器,请分享它。

穆罕默德·萨迪克

干得好

//  Converted to Swift 5.2 by Swiftify v5.2.18740 - https://swiftify.com/
let iconView = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 50))
iconView.image = UIImage(named: "my-icon-image")

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 35, height: 50))
label.text = "test"
iconView.addSubview(label)

UIGraphicsBeginImageContextWithOptions(label.bounds.size, _: false, _: UIScreen.main.scale)
if let context = UIGraphicsGetCurrentContext() {
    iconView.layer.render(in: context)
}
let icon = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(lat, lng)
marker.title = "my title"
marker.icon = icon

您可以使用https://swiftify.com/#/converter/code/获取一小段代码

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章