【问题标题】:Convert swift repeating array in objective C在目标C中转换快速重复数组
【发布时间】:2016-11-13 14:59:25
【问题描述】:

我有一个快速的项目。我被困在一条线上以转换目标 c

var bezierPoints = [CGPoint](repeating: CGPoint(), count: 5)

如何在 Objective C 中创建数组?

【问题讨论】:

  • C 数组 CGPoint bezierPoints[5]; 能满足您的需求吗?
  • 最佳方法取决于您实际尝试执行的操作。很可能,您根本不需要这条线。
  • @MartinR 是的,它满足...谢谢

标签: objective-c swift


【解决方案1】:

在 Objective-C 中,您不能创建 CGPoint(s) 的数组,因为 NSArray 只能包含 对象

无论如何,您都可以将CGPoint 包装成NSValue 所以

NSArray * points = [NSArray arrayWithObjects:
                    [NSValue valueWithCGPoint:CGPointZero],
                    [NSValue valueWithCGPoint:CGPointZero],
                    [NSValue valueWithCGPoint:CGPointZero],
                    [NSValue valueWithCGPoint:CGPointZero],
                    [NSValue valueWithCGPoint:CGPointZero],
                    nil];

或者按照 @Alexander Momchliov 的建议

NSArray * points = @[
                     [NSValue valueWithCGPoint:CGPointZero],
                     [NSValue valueWithCGPoint:CGPointZero],
                     [NSValue valueWithCGPoint:CGPointZero],
                     [NSValue valueWithCGPoint:CGPointZero],
                     [NSValue valueWithCGPoint:CGPointZero],
                    ];

【讨论】:

  • 为什么不使用数组字面量? @[a, b, c]
  • @AlexanderMomchliov:因为我忘记了 Objective-C :D 谢谢!
猜你喜欢
  • 1970-01-01
  • 2020-05-06
  • 1970-01-01
  • 1970-01-01
  • 2013-03-11
  • 1970-01-01
  • 2018-07-10
  • 2015-08-25
  • 2017-05-12
相关资源
最近更新 更多