关键词:
【中文标题】iOS 12.0 替代使用已弃用的 archiveRootObject:toFile:【英文标题】:iOS 12.0 Alternative to Using Deprecated archiveRootObject:toFile: 【发布时间】:2019-05-03 22:47:12 【问题描述】:在 iOS 12 中,archiveRootObject:toFile:
已被弃用。任何人都可以提出一种简化的替代方案来将对象归档到文件中吗?
//Generic example of archiver prior to iOS 12.0
-(BOOL) archive:(id)archiveObject withFileName:(NSString*)filename
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
return [NSKeyedArchiver archiveRootObject:archiveObject toFile:path];
【问题讨论】:
【参考方案1】:感谢@vadian 的提示,这是我想出的在 iOS 12 下进行归档和取消归档的方法:
NSError *error = nil;
NSString *docsDir;
NSArray *dirPaths;
//Get the device's data directory:
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"appData.data"]];
//Archive using iOS 12 compliant coding:
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:@"foo" requiringSecureCoding:NO error:&error];
[data writeToFile:databasePath options:NSDataWritingAtomic error:&error];
NSLog(@"Write returned error: %@", [error localizedDescription]);
//Unarchive the data:
NSData *newData = [NSData dataWithContentsOfFile:databasePath];
NSString *fooString = [NSKeyedUnarchiver unarchivedObjectOfClass:[NSString class] fromData:newData error:&error];
【讨论】:
您(或其他任何人)是否设法通过 requiresSecureCoding:YES 来实现这一点? 这里,如果您在 NSString 格式中保存数据,请使用 [NSString 类],或者如果您在 NSDictionary 格式中保存数据,请在 unarchivedObjectOfClass 中使用 [NSDictionary 类]:【参考方案2】:unArchivedObjectOfClass 在尝试解码未使用安全编码的对象时向我抛出了错误。经过多次试验和错误,这终于在没有触发 iOS 12/13 弃用警告的情况下奏效了:
// Archive the object
NSData* data = [NSKeyedArchiver archivedDataWithRootObject:theObject requiringSecureCoding:NO error:nil];
// Unarchive the object
NSKeyedUnarchiver* unarchiver = [[NSKeyedUnarchiver alloc] initForReadingFromData:data error:nil];
unarchiver.requiresSecureCoding = NO;
id theCopy = [unarchiver decodeTopLevelObjectForKey:NSKeyedArchiveRootObjectKey error:nil];
【讨论】:
【参考方案3】:作为suggested by Apple,我们应该使用FileManager来读取/写入归档文件。
func archiveURL() -> URL?
guard let documentURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
else return nil
return documentURL.appendingPathComponent("MyArchive.data")
func archive(customObject: CustomObject)
guard let dataToBeArchived = try? NSKeyedArchiver.archivedData(withRootObject: customObject, requiringSecureCoding: true),
let archiveURL = archiveURL()
else
return
try? dataToBeArchived.write(to: archiveURL)
func unarchive() -> CustomObject?
guard let archiveURL = archiveURL(),
let archivedData = try? Data(contentsOf: archiveURL),
let customObject = (try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(archivedData)) as? CustomObject
else
return nil
return customObject
【讨论】:
【参考方案4】:替换为archivedDataWithRootObject:requiringSecureCoding:error:
+ (NSData *)archivedDataWithRootObject:(id)object
requiringSecureCoding:(BOOL)requiresSecureCoding
error:(NSError * _Nullable *)error;
加上一个额外的步骤将数据写入磁盘。
请看Foundation iOS 11.4 to 12.0 API Differences
【讨论】:
已弃用的 google plus api 的替代解决方案是啥?
】已弃用的googleplusapi的替代解决方案是啥?【英文标题】:Whatisthealternativesolutionofdeprecatedgoogleplusapi?已弃用的googleplusapi的替代解决方案是什么?【发布时间】:2019-06-2315:43:08【问题描述】:Google宣布在3月7日弃用所有googleplusapi... 查看详情
是否有快速替代已弃用的“SKPaymentTransaction.transactionReceipt”?
】是否有快速替代已弃用的“SKPaymentTransaction.transactionReceipt”?【英文标题】:Isthereafastreplacementfordeprecate`SKPaymentTransaction.transactionReceipt`?【发布时间】:2014-05-1513:41:45【问题描述】:是否有快速替代已弃用的SKPaymentTransaction.tran... 查看详情
BitmapDrawable 已弃用的替代方案
】BitmapDrawable已弃用的替代方案【英文标题】:BitmapDrawabledeprecatedalternative【发布时间】:2012-04-1606:11:56【问题描述】:我有以下代码可以将可绘制对象旋转一定的度数。publicDrawablerotateDrawable(floatangle,Contextcontext)BitmaparrowBitmap=Bitm... 查看详情
替代已弃用的 AudioManager.isWiredHeadsetOn?
】替代已弃用的AudioManager.isWiredHeadsetOn?【英文标题】:AlternativetothedeprecatedAudioManager.isWiredHeadsetOn?【发布时间】:2013-01-0207:43:44【问题描述】:方法AudioManager.isWiredHeadsetOn()从api级别14被弃用,我们现在如何检测是否连接了有线... 查看详情
已弃用的 NSURLConnection 方法,有替代方法吗?
】已弃用的NSURLConnection方法,有替代方法吗?【英文标题】:DeprecatedNSURLConnectionMethods,isthereanalternative?【发布时间】:2012-02-2814:12:26【问题描述】:我正在设置一个NSURLConnection来访问远程服务器:NSURL*url=[[NSURLalloc]initWithString:proj... 查看详情
如何为已弃用的英特尔 IPP API 找到替代 API?
】如何为已弃用的英特尔IPPAPI找到替代API?【英文标题】:HowtofindalternativeAPIsfordeprecatedIntelIPPAPIs?【发布时间】:2014-11-0410:11:49【问题描述】:我正在与IntelIntegratedPerformancePrimitives(IntelIPP8.2)合作。我正在尝试使用来自UtilityFunction... 查看详情
寻找现在已弃用的 retryWhen 的替代方案
】寻找现在已弃用的retryWhen的替代方案【英文标题】:LookingforanalternativeofretryWhenwhichisnowDeprecated【发布时间】:2020-10-0616:34:43【问题描述】:我遇到了WebClient和reactor-extra的问题。确实,我有以下方法:publicEmployeegetEmployee(Stringemp... 查看详情
图像中已弃用的 Notification 类的替代方法是啥?
】图像中已弃用的Notification类的替代方法是啥?【英文标题】:WhatisthealternativeforthedeprecatedNotificationclassintheimage?图像中已弃用的Notification类的替代方法是什么?【发布时间】:2014-02-1811:14:20【问题描述】:我打算在android设备的... 查看详情
channel 或 mutablesharedflow ,哪个是已弃用的 localbroadcastmanager 的更好替代品
...l或mutablesharedflow,哪个是已弃用的localbroadcastmanager的更好替代品【英文标题】:channelormutablesharedflow,whichoneisabetterreplacementfordeprecatedlocalbroadcastmanager【发布时间】:2021-04-0321:15:20【问题描述】:过去,我在聊天和出租车应用程序... 查看详情
pymxs 替代已弃用的 MaxPlus 实用程序函数
】pymxs替代已弃用的MaxPlus实用程序函数【英文标题】:pymxsalternativestothedeprecatedMaxPlusutilityfunctions【发布时间】:2021-08-2400:11:17【问题描述】:随着Autodesk从3dsMax中删除MaxPlus,我现在不得不重新编写一些代码,并且想知道是否有... 查看详情
替代已弃用的 setup_environ() 一次性 django 脚本?
】替代已弃用的setup_environ()一次性django脚本?【英文标题】:Alternativetothedeprecatedsetup_environ()forone-offdjangoscripts?【发布时间】:2013-02-0912:48:39【问题描述】:不久前我使用setup_environ()编写了一个一次性的python脚本,该脚本可以从... 查看详情
替代已弃用的 setup_environ() 一次性 django 脚本?
】替代已弃用的setup_environ()一次性django脚本?【英文标题】:Alternativetothedeprecatedsetup_environ()forone-offdjangoscripts?【发布时间】:2013-02-0912:48:39【问题描述】:不久前我使用setup_environ()编写了一个一次性的python脚本,该脚本可以从... 查看详情
已弃用的 Hibernate.createClob(Reader reader, int length) 的替代方法是啥
】已弃用的Hibernate.createClob(Readerreader,intlength)的替代方法是啥【英文标题】:WhatisthealternatefordeprecatedHibernate.createClob(Readerreader,intlength)已弃用的Hibernate.createClob(Readerreader,intlength)的替代方法是什么【发布时间】:2012-02-1601:23:11【 查看详情
Android:替代已弃用的 Context.MODE_WORLD_READABLE?
】Android:替代已弃用的Context.MODE_WORLD_READABLE?【英文标题】:Android:AlternativetothedeprecatedContext.MODE_WORLD_READABLE?【发布时间】:2012-12-0103:51:09【问题描述】:Fromhere我知道一种写入文件并可供其他应用程序和其他意图访问的方法,... 查看详情
[FFmpeg]啥是已弃用的 avpicture_alloc、avpicture::data 的替代品
】[FFmpeg]啥是已弃用的avpicture_alloc、avpicture::data的替代品【英文标题】:[FFmpeg]whatisreplacementsofavpicture_alloc,avpicture::datawhichweredeprecated[FFmpeg]什么是已弃用的avpicture_alloc、avpicture::data的替代品【发布时间】:2016-04-1204:47:35【问题描... 查看详情
Alamofire 已弃用的代码
...我将其追溯到Alamofire中的以下语句。我没有看到任何关于替代品的提及。@available(*,deprecated=3.4.0)publicstaticfuncerrorWithCode( 查看详情
Google Sceneform – 是不是已弃用?有替代品吗? [关闭]
】GoogleSceneform–是不是已弃用?有替代品吗?[关闭]【英文标题】:GoogleSceneform–Isitdeprecated?Anyreplacement?[closed]GoogleSceneform–是否已弃用?有替代品吗?[关闭]【发布时间】:2020-10-0817:05:43【问题描述】:我在我的ARCore项目Sceneform... 查看详情
在 iOS7 中,我应该使用啥来代替已弃用的 GKLeaderboardViewController?
】在iOS7中,我应该使用啥来代替已弃用的GKLeaderboardViewController?【英文标题】:WhatshouldIuseinsteadofdeprecatedGKLeaderboardViewControlleriniOS7?在iOS7中,我应该使用什么来代替已弃用的GKLeaderboardViewController?【发布时间】:2013-10-0217:18:44【... 查看详情