2015-01-13 175 views
1

我是Swift和iOS开发中的新成员。最近我试图用Swift编写一个使用CorePlot(用ObjectiveC编写)的iOS应用程序。我试图在Swift中重写一个CorePlot iOS示例代码(在http://www.raywenderlich.com/13271/how-to-draw-graphs-with-core-plot-part-2中找到)。有这样一行样品的ObjectiveC:ObjectiveC mutableCopy如何快速运行

CPTMutableLineStyle *aaplLineStyle = [aaplPlot.dataLineStyle mutableCopy]; 
aaplLineStyle.lineWidth = 2.5; 

,我试图重写这样的:

var lineStyle = aaplPlot.dataLineStyle.mutableCopy(); 
lineStyle.lineColor = aaplColor; 

的问题是我得到了我的代码斯威夫特版本说

编译错误
"cannot assign to 'lineColor' in 'lineStyle'". 

我也得到警告说,

"variable lineStyle inferred to have type 'AnyObject', which may be unexpected" 

'applePlot',顺便说一下,是的ObjectiveC这样定义的类型CPTLineStyle的:

@interface CPTLineStyle : NSObject<NSCoding, NSCopying, NSMutableCopying> 

@property (nonatomic, readonly) CGLineCap lineCap; 
@property (nonatomic, readonly) CGLineJoin lineJoin; 
@property (nonatomic, readonly) CGFloat miterLimit; 
@property (nonatomic, readonly) CGFloat lineWidth; 
@property (nonatomic, readonly) NSArray *dashPattern; 
@property (nonatomic, readonly) CGFloat patternPhase; 
@property (nonatomic, readonly) CPTColor *lineColor; 
@property (nonatomic, readonly) CPTFill *lineFill; 
@property (nonatomic, readonly) CPTGradient *lineGradient; 
@property (nonatomic, readonly, getter = isOpaque) BOOL opaque; 

/// @name Factory Methods 
/// @{ 
+(instancetype)lineStyle; 
/// @} 

/// @name Drawing 
/// @{ 
-(void)setLineStyleInContext:(CGContextRef)context; 
-(void)strokePathInContext:(CGContextRef)context; 
-(void)strokeRect:(CGRect)rect inContext:(CGContextRef)context; 
/// @} 

@end 

任何机构可以告诉我,如果我做错了什么?我如何正确地重写那些代码行?

感谢

回答

5

的问题是,mutableCopy返回AnyObject。一个AnyObject没有属性,所以你不能分配到它的属性。您需要将其转换为CPTMutableLineStyle。

var lineStyle = aaplPlot.dataLineStyle.mutableCopy() as CPTMutableLineStyle 
lineStyle.lineColor = aaplColor; 
+0

再次感谢马特。我最终发现它,所以我删除了我以前的评论,询问CPTMutableLineStyle在哪里,因为我没有看到您的评论回复它。 – trungdinhtrong