2015-01-13 89 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