gpt4 book ai didi

objective-c - iOS - 努力在 Obj-C/Swift 代码之间转换

转载 作者:行者123 更新时间:2023-11-28 06:09:28 26 4
gpt4 key购买 nike

我一直在将我的应用程序的某些部分从 Obj-C 翻译成 Swift,但我遇到了一个我无法理解或绕过的问题。

看看这个 Objective-C 类:

模型类预测

@class City;

@interface Forecast : ForecastInterface

@property (nonatomic, retain) City *city;

@end

模型类 Forecast48h

@class City;

@interface Forecast48h : ForecastInterface

@property (nonatomic, retain) City *city;

@end

模型类总结

@class City;

@interface Summary : ForecastInterface

@property (nonatomic, retain) City *city;

@end

最后

模型预测界面

@interface ForecastInterface : NSManagedObject

@property (nonatomic, retain) NSDate * date;
@property (nonatomic, retain) NSString * descriptionLabel;
@property (nonatomic, retain) NSString * windDirection;
@property (nonatomic, retain) NSString * seaState;
@property (nonatomic, retain) NSNumber * day;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * probFreeze;
@property (nonatomic, retain) NSString * probSnow;
@property (nonatomic, retain) NSString * probRain;
@property (nonatomic, retain) NSNumber * tempMap;
@property (nonatomic, retain) NSString * tmpMax;
@property (nonatomic, retain) NSNumber * tmpSea;
@property (nonatomic, retain) NSString * tmpMin;
@property (nonatomic, retain) NSString * speedWind;
@property (nonatomic, retain) NSString * endDate;
@property (nonatomic, retain) NSString * startDate;

@end

好的,现在您已经有了我的 CoreData 模型的演示。

我想了解为什么我不能将 Forecast 或 Forecast48h 类型转换为 Summary 类型?

看看这个例子:

var listForecasts = Array<Any>()

self.listForecasts = city.bestForecastArray()

这是一个 Objective-C 方法,用于获取一些 Forecast 或 Forecast48 对象并返回一个 NSArray。

-(NSArray *)bestForecastArray;

现在是我挣扎的部分。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as? MyCollectionViewCell
cell?.layoutIfNeeded()

var tmpForecast = self.listForecasts[indexPath.row]

cell?.delegate = self
cell?.load(tmpForecast as! Summary)

return cell!
}

当我转储 listForecasts 时,它只包含 Forecast 和 Forecast48h 对象。

当我想加载我的单元格时,出现运行时错误:

Could not cast value of type 'Forecast48h_Forecast48h_' to 'Summary'

首先,为什么在错误消息中输入这个双 Forecast48h?

加载方法的原型(prototype):

-(void)loadCell:(Summary*)weatherSummary;

我确实需要将摘要对象传递给 loadCell 方法。为什么这在 Objective-C 中被接受但在 Swift 中不被接受?

看看这段用 Objective-C 编写的代码:

Summary* aSummary = [_listForecasts objectAtIndex:indexPath.row];
[cell setDelegate:self];
[cell loadCell:aSummary];
return cell;

这在 Objective-C 中有效。

有什么帮助吗?不要犹豫,询问更多信息。该项目很复杂,难以解释我尽量使其尽可能简单。

编辑:

@interface Summary (Additions)

+(NSArray*)allSummaryOfCity:(NSManagedObjectContext *)context city:(City*)aCity;

+(NSArray *)arrayInformationsWithSummary:(Summary*)aSummary;

+(NSFetchedResultsController *)fetchControllerForAllSummaryOfCity:(NSManagedObjectContext *)context cityIndicatif:(NSString*)anIndicatifCity;
@end

最佳答案

多态性意味着您始终可以将一个对象转换为其父类(super class)类型,但如果两个对象具有从同一父类(super class)派生的类型,则您不能从一种类型转换为另一种类型。在协议(protocol)的情况下,同样有效。

在您的情况下,使用 ForecastInterface 类型就足够了,因为您确定任何获取的对象都实现了 ForecastInterface 协议(protocol):

var listForecasts = Array<ForecastInterface>()
self.listForecasts = city.bestForecastArray()

...

-(void)loadCell:(ForecastInterface*)weatherSummary;

...


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as? MyCollectionViewCell
cell?.layoutIfNeeded()

// tmpForecast type: ForeCastInterface
var tmpForecast = self.listForecasts[indexPath.row]

cell?.delegate = self
cell?.load(tmpForecast)

return cell!
}

以处理ForecastInterface 类型而不是Summary 类型的方式更改您的方法。

编辑

如果您需要使用仅在Summary 类中的方法,那么您有两个解决方案:(1) 将这些方法移动到ForeCastInterface 协议(protocol)中,或者 ( 2) 尝试转换为 Summary 并仅在对象确实属于 Summary 类型时调用方法,而不强制转换:

var tmpForecast = self.listForecasts[indexPath.row]
if let summary = tmpForecast as? Summary {
...
}

关于objective-c - iOS - 努力在 Obj-C/Swift 代码之间转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47156479/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com