Removing Suite in UserDefault

我们两清 提交于 2020-01-15 12:20:08

问题


var sharedPreference    : UserDefaults  = UserDefaults.init(suiteName: "userKeyValue")!

I am creating UserDefault instance like this and most probably it is creating a space in device with name userKeyValue and keep all key value pairs in that.

Now I want to migrate the content to different UserDefault instance

var oldSharedPreference : UserDefaults? = UserDefaults(suiteName: "userKeyValue")
if (oldSharedPreference != nil) {
  print("Previous data found")
  for (key, value) in oldSharedPreference!.dictionaryRepresentation() {
    sharedPreference.set(value, forKey: key)
  }
  sharedPreference.synchronize()
  oldSharedPreference!.removeSuite(named: "userKeyValue")
  oldSharedPreference!.synchronize()
} 

I installed the app many times in one after another but every time I am getting the log Previous data found. I dont know if i am doing it in correct way or not.

I just want that existence of UserDefaults instance of userKeyValue should no longer exists once this piece of code runs but this is not happening.

UPDATE:: I am getting this content in UserDefault when its instance is created

["AKLastCheckInSuccessDate": 2019-06-03 09:06:11 +0000, "NSLanguages": <__NSArrayI 0x2820289c0>(
en-IN,
en
)
, "com.apple.content-rating.TVShowRating": 1000, "com.apple.content-rating.ExplicitBooksAllowed": 1, "AppleLanguagesDidMigrate": 16E227, "INNextHearbeatDate": 581574895.850746, "AppleLocale": en_IN, "NSInterfaceStyle": macintosh, "AKLastCheckInAttemptDate": 2019-06-09 20:42:49 +0000, "AddingEmojiKeybordHandled": 1, "com.apple.content-rating.MovieRating": 1000, "AKLastIDMSEnvironment": 0, "AKLastEmailListRequestDateKey": 2019-06-03 09:04:39 +0000, "AppleLanguages": <__NSCFArray 0x283579f80>(
en-IN
)
, "com.apple.content-rating.AppRating": 1000, "com.apple.content-rating.ExplicitMusicPodcastsAllowed": 0, "PKKeychainVersionKey": 4, "AppleKeyboards": <__NSCFArray 0x28357a080>(
en_IN,
emoji
)
, "AppleITunesStoreItemKinds": <__NSCFArray 0x281531080>(
itunes-u,
movie,
ringtone,
album,
software-update,
booklet,
tone,
music-video,
song,
podcast,
software,
podcast-episode,
wemix,
eBook,

artist,
mix,
document
)
, "INNextFreshmintRefreshDateKey": 581904664.355094, "ApplePasscodeKeyboards": <__NSCFArray 0x28357a0c0>(
en_IN@sw=QWERTY;hw=Automatic,
emoji@sw=Emoji
)
, "AppleKeyboardsExpanded": 1]

回答1:


try below mentioned code for removing UserDefaults instance

UserDefaults.standard.removeObject(forKey: "userKeyValue")



回答2:


Did you try to remove the whole domain?

UserDefaults.standard.removePersistentDomain(forName: "userKeyValue")
UserDefaults.standard.synchronize()

From Apple documentation

Calling this method is equivalent to initializing a user defaults object with init(suiteName:) passing domainName, and calling the removeObject(forKey:) method on each of its keys.



来源:https://stackoverflow.com/questions/56523845/removing-suite-in-userdefault

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!