V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
ericgui
V2EX  ›  iDev

请教: AppStorage 的设置,怎么重置 ?

  •  
  •   ericgui · 2021-02-04 13:48:41 +08:00 · 2712 次点击
    这是一个创建于 1148 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我在 AppStorage 里,有一个变量,值为 true,第一次点击按钮之后,就变为 false

    结果每次重新 build 之后,这个值就永远为 false 了

    目前只好在 emulator 里,把 app 删了,重新 run 一下

    有什么办法仅仅重置 AppStorage 而不用删 app ? 谢谢
    6 条回复    2021-02-05 12:11:20 +08:00
    AllenChiangCN
        1
    AllenChiangCN  
       2021-02-04 14:04:19 +08:00
    进 app info,然后 clear storage
    AllenChiangCN
        2
    AllenChiangCN  
       2021-02-04 14:04:57 +08:00
    @AllenChiangCN 不好意思,你好像说的是 iOS
    ericgui
        3
    ericgui  
    OP
       2021-02-04 14:07:31 +08:00
    @AllenChiangCN 是 iOS,不好意思没说清楚
    JasonEWNL
        4
    JasonEWNL  
       2021-02-05 00:28:28 +08:00   ❤️ 1
    标记为 AppStorage 的变量本地存着, run 之后就像常规覆盖安装后持续保存的用户数据一样.

    方案一: 设置为 State, 小量可在打包前手动改回, 如果比较重度建议设置预处理语句.

    ```swift
    #if DEBUG
    @State ...
    #else
    @AppStorage ...
    #endif
    ```

    方案二: 参考 Stack Overflow 的一个个曲线方案 (针对模拟器的在评论里): https://stackoverflow.com/questions/15258777/xcode-delete-application-before-running-on-device#answer-32992734

    简单解释就是把 `xcrun simctl uninstall sim-udid bundle-id` 放在 build 脚本里, 每次 build 会先删掉应用.
    ericgui
        5
    ericgui  
    OP
       2021-02-05 01:26:11 +08:00
    @JasonEWNL 谢谢
    也就是说,其实没办法避免删除 app 这个步骤。。。。无非是手动还是用脚本命令
    JasonEWNL
        6
    JasonEWNL  
       2021-02-05 12:11:20 +08:00
    @ericgui 是的. 因为不删除地在 Xcode 里 run 一次, 就像用户不删除应用就从 App Store 更新一样, 标记为 AppStorage 的变量存储在 UserDefaults 里, 如其他永久存储方案一样, 仅能通过用户操作 (包括删除在内) 才能修改或重置.

    不过这么一说就想到方案三: 给标记为 AppStorage 的变量所在 View 的 body 设置 onAppear, 类似方案一地进行预处理.

    ```swift
    @AppStorage ...

    var body: some View {
    ...
    }
    .onAppear {
    #if DEBUG
    ... = DEBUG_VALUE
    #endif
    }
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1045 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 22:39 · PVG 06:39 · LAX 15:39 · JFK 18:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.