如何将UIApplicationDelegateAdaptor用作ObservableObject?

时间:2020-10-22 14:44:27

标签: ios swift swiftui appdelegate ios14

在我的iOS 14 @bot.command() async def serverinfo(ctx): guild = ctx.guild server = discord.Embed(title="Server info", description="▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬") server.add_field(name=f':mega: Server:', value=f'➜ Server name: {guild.name}\n➜ Server id: {guild.id}\n➜ Created at: {guild.created_at}', inline=False) server.add_field(name=f':crown: Owner', value=f'➜ Name: {guild.owner.display_name}\n➜ Mention: {guild.owner.mention}\n➜ ID: {guild.owner.id}', inline=False) server.add_field(name=f':earth_americas: Region:', value=f'➜ {guild.region}') server.add_field(name=f':busts_in_silhouette: Members:', value=f'➜ Total Members: {len(guild.members)}\n➜ Online <:online:768523616336871434>: {len(list(filter(lambda m: str(m.status) == "online", ctx.guild.members)))}\n➜ Offline <:offline:768523674067533835>: {len(list(filter(lambda m: str(m.status) == "offline", ctx.guild.members)))}\n➜ Do not disturb <:dnd:768526277845188608>: {len(list(filter(lambda m: str(m.status) == "dnd", ctx.guild.members)))}\n➜ Idle <:idle:768523706217267252>: {len(list(filter(lambda m: str(m.status) == "idle", ctx.guild.members)))}', inline=False) server.add_field(name=f':shield: Roles:', value=f'➜ Total roles: {len(guild.roles)}\n➜ Top Role: <@&{ctx.guild.roles[len(ctx.guild.roles)-1].id}>') server.add_field(name=f':file_cabinet: Channels:', value=f'➜ Total channels: {len(guild.channels)}\n➜ Categories: {len(guild.categories)}\n➜ Text Channels: {len(guild.text_channels)}\n➜ Voice Channels: {len(guild.voice_channels)}\n➜ AFK channel: {guild.afk_channel}') server.add_field(name=f':notebook_with_decorative_cover: Emojis:', value=f'➜ Total emojis: {len(guild.emojis)}') server.set_thumbnail(url=f'{ctx.guild.icon_url}') await ctx.message.delete() await ctx.channel.send(embed=server) 中,我可以通过以下方式注册旧版App

AppDelegate

但是,我注意到您在the documentation中可以将@main struct MyApp: App { #if os(iOS) @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate #endif var body: some Scene { ... } } #if os(iOS) class AppDelegate: NSObject, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool { SomeSDK.configure(with: launchOptions) return true } } #endif 设为UIApplicationDelegateAdaptor,然后将其注入ObservableObject中:

... delegate将放置在环境中,并且可以使用视图层次结构中的EnvironmentObject属性包装器进行访问。

我找不到任何如何工作的示例。使此功能@EnvironmentObject起作用的语法是什么?

1 个答案:

答案 0 :(得分:3)

这是用法演示

  1. 确认PodAppDelegate
ObservableObject
  1. 像以前一样将class AppDelegate: NSObject, UIApplicationDelegate, ObservableObject { @Published var value: String = "" func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { self.value = "test" // in any callback use your published property return true } } 注册为适配器
AppDelegate
    在您的某些视图中
  1. 在需要的地方声明@main struct Testing_SwiftUI2App: App { @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate // ... other code
@EnvironmentObject

通过Xcode 12 / iOS 14测试。