├── README.md ├── UIViewRepresentableBug.png ├── UIViewRepresentableBug.xcodeproj └── project.pbxproj └── UIViewRepresentableBug ├── AppDelegate.swift ├── Assets.xcassets ├── AppIcon.appiconset │ └── Contents.json └── Contents.json ├── Base.lproj └── LaunchScreen.storyboard ├── ContentView.swift ├── FinalClassUIViewRepresentable.swift ├── Info.plist ├── Preview Content └── Preview Assets.xcassets │ └── Contents.json ├── SceneDelegate.swift ├── StructUIViewRepresentable.swift └── ViewModel.swift /README.md: -------------------------------------------------------------------------------- 1 | ## SwiftUI bug 2 | 3 | ![UIViewRepresentableBug](UIViewRepresentableBug.png) 4 | 5 | As of Xcode 11.3.1 and 11.4-beta, there is a bug with updating a UIViewRepresentable. When a SwiftUI View passes an 6 | `@ObservedObject` to a UIViewRepresentable, and the UIViewRepresentable is a `struct`, SwiftUI fails to call the 7 | `updateUIView` method of the UIViewRepresentable when the observed object changes. 8 | 9 | If the UIViewRepresentable is a `final class`, then its `updateUIView` is correctly called when the observable 10 | object changes. 11 | 12 | ### Install and run 13 | 14 | This should be ready to go: just clone this repo and run it in Xcode 11.3.1 or 11.4-beta. 15 | 16 | ### Test structure 17 | 18 | - A SwiftUI View with an `@ObservedObject`. 19 | - The `@ObservedObject` changes every 1 second. 20 | - The SwiftUI View's body contains two UIViewRepresentables: 21 | - one is a `struct` 22 | - the other one is a `final class` 23 | - The SwiftUI View passes its `@ObservedObject` to the two UIViewRepresentables. 24 | 25 | ### Expected behavior 26 | 27 | - Both UIViewRepresentables update every 1 second. 28 | 29 | ### Actual behavior 30 | 31 | - The `final class` UIViewRepresentable updates correctly every 1 second. 32 | - The `struct` UIViewRepresentable does NOT update. 33 | 34 | Side note: the `struct` UIViewRepresentable does update when the device orientation is changed. 35 | -------------------------------------------------------------------------------- /UIViewRepresentableBug.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mediweb/UIViewRepresentableBug/a4a239aabe58f75b99cf3fd8b735c56ccfaa2fe1/UIViewRepresentableBug.png -------------------------------------------------------------------------------- /UIViewRepresentableBug.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | F5C080B023FB485D003725AB /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5C080AF23FB485D003725AB /* AppDelegate.swift */; }; 11 | F5C080B223FB485D003725AB /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5C080B123FB485D003725AB /* SceneDelegate.swift */; }; 12 | F5C080B423FB485D003725AB /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5C080B323FB485D003725AB /* ContentView.swift */; }; 13 | F5C080B623FB4861003725AB /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F5C080B523FB4861003725AB /* Assets.xcassets */; }; 14 | F5C080B923FB4861003725AB /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F5C080B823FB4861003725AB /* Preview Assets.xcassets */; }; 15 | F5C080BC23FB4861003725AB /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F5C080BA23FB4861003725AB /* LaunchScreen.storyboard */; }; 16 | F5C080C423FB48F9003725AB /* ViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5C080C323FB48F9003725AB /* ViewModel.swift */; }; 17 | F5C080C623FB494C003725AB /* StructUIViewRepresentable.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5C080C523FB494C003725AB /* StructUIViewRepresentable.swift */; }; 18 | F5C080C823FB49EA003725AB /* FinalClassUIViewRepresentable.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5C080C723FB49EA003725AB /* FinalClassUIViewRepresentable.swift */; }; 19 | /* End PBXBuildFile section */ 20 | 21 | /* Begin PBXFileReference section */ 22 | F5C080AC23FB485D003725AB /* UIViewRepresentableBug.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = UIViewRepresentableBug.app; sourceTree = BUILT_PRODUCTS_DIR; }; 23 | F5C080AF23FB485D003725AB /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 24 | F5C080B123FB485D003725AB /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; 25 | F5C080B323FB485D003725AB /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; 26 | F5C080B523FB4861003725AB /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 27 | F5C080B823FB4861003725AB /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; 28 | F5C080BB23FB4861003725AB /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 29 | F5C080BD23FB4861003725AB /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 30 | F5C080C323FB48F9003725AB /* ViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewModel.swift; sourceTree = ""; }; 31 | F5C080C523FB494C003725AB /* StructUIViewRepresentable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StructUIViewRepresentable.swift; sourceTree = ""; }; 32 | F5C080C723FB49EA003725AB /* FinalClassUIViewRepresentable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FinalClassUIViewRepresentable.swift; sourceTree = ""; }; 33 | F5C080C923FB516F003725AB /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; 34 | /* End PBXFileReference section */ 35 | 36 | /* Begin PBXFrameworksBuildPhase section */ 37 | F5C080A923FB485D003725AB /* Frameworks */ = { 38 | isa = PBXFrameworksBuildPhase; 39 | buildActionMask = 2147483647; 40 | files = ( 41 | ); 42 | runOnlyForDeploymentPostprocessing = 0; 43 | }; 44 | /* End PBXFrameworksBuildPhase section */ 45 | 46 | /* Begin PBXGroup section */ 47 | F5C080A323FB485D003725AB = { 48 | isa = PBXGroup; 49 | children = ( 50 | F5C080C923FB516F003725AB /* README.md */, 51 | F5C080AE23FB485D003725AB /* UIViewRepresentableBug */, 52 | F5C080AD23FB485D003725AB /* Products */, 53 | ); 54 | sourceTree = ""; 55 | }; 56 | F5C080AD23FB485D003725AB /* Products */ = { 57 | isa = PBXGroup; 58 | children = ( 59 | F5C080AC23FB485D003725AB /* UIViewRepresentableBug.app */, 60 | ); 61 | name = Products; 62 | sourceTree = ""; 63 | }; 64 | F5C080AE23FB485D003725AB /* UIViewRepresentableBug */ = { 65 | isa = PBXGroup; 66 | children = ( 67 | F5C080AF23FB485D003725AB /* AppDelegate.swift */, 68 | F5C080B123FB485D003725AB /* SceneDelegate.swift */, 69 | F5C080B323FB485D003725AB /* ContentView.swift */, 70 | F5C080C523FB494C003725AB /* StructUIViewRepresentable.swift */, 71 | F5C080C723FB49EA003725AB /* FinalClassUIViewRepresentable.swift */, 72 | F5C080C323FB48F9003725AB /* ViewModel.swift */, 73 | F5C080B523FB4861003725AB /* Assets.xcassets */, 74 | F5C080BA23FB4861003725AB /* LaunchScreen.storyboard */, 75 | F5C080BD23FB4861003725AB /* Info.plist */, 76 | F5C080B723FB4861003725AB /* Preview Content */, 77 | ); 78 | path = UIViewRepresentableBug; 79 | sourceTree = ""; 80 | }; 81 | F5C080B723FB4861003725AB /* Preview Content */ = { 82 | isa = PBXGroup; 83 | children = ( 84 | F5C080B823FB4861003725AB /* Preview Assets.xcassets */, 85 | ); 86 | path = "Preview Content"; 87 | sourceTree = ""; 88 | }; 89 | /* End PBXGroup section */ 90 | 91 | /* Begin PBXNativeTarget section */ 92 | F5C080AB23FB485D003725AB /* UIViewRepresentableBug */ = { 93 | isa = PBXNativeTarget; 94 | buildConfigurationList = F5C080C023FB4861003725AB /* Build configuration list for PBXNativeTarget "UIViewRepresentableBug" */; 95 | buildPhases = ( 96 | F5C080A823FB485D003725AB /* Sources */, 97 | F5C080A923FB485D003725AB /* Frameworks */, 98 | F5C080AA23FB485D003725AB /* Resources */, 99 | ); 100 | buildRules = ( 101 | ); 102 | dependencies = ( 103 | ); 104 | name = UIViewRepresentableBug; 105 | productName = UIViewRepresentableBug; 106 | productReference = F5C080AC23FB485D003725AB /* UIViewRepresentableBug.app */; 107 | productType = "com.apple.product-type.application"; 108 | }; 109 | /* End PBXNativeTarget section */ 110 | 111 | /* Begin PBXProject section */ 112 | F5C080A423FB485D003725AB /* Project object */ = { 113 | isa = PBXProject; 114 | attributes = { 115 | LastSwiftUpdateCheck = 1130; 116 | LastUpgradeCheck = 1130; 117 | ORGANIZATIONNAME = MediWeb; 118 | TargetAttributes = { 119 | F5C080AB23FB485D003725AB = { 120 | CreatedOnToolsVersion = 11.3.1; 121 | }; 122 | }; 123 | }; 124 | buildConfigurationList = F5C080A723FB485D003725AB /* Build configuration list for PBXProject "UIViewRepresentableBug" */; 125 | compatibilityVersion = "Xcode 9.3"; 126 | developmentRegion = en; 127 | hasScannedForEncodings = 0; 128 | knownRegions = ( 129 | en, 130 | Base, 131 | ); 132 | mainGroup = F5C080A323FB485D003725AB; 133 | productRefGroup = F5C080AD23FB485D003725AB /* Products */; 134 | projectDirPath = ""; 135 | projectRoot = ""; 136 | targets = ( 137 | F5C080AB23FB485D003725AB /* UIViewRepresentableBug */, 138 | ); 139 | }; 140 | /* End PBXProject section */ 141 | 142 | /* Begin PBXResourcesBuildPhase section */ 143 | F5C080AA23FB485D003725AB /* Resources */ = { 144 | isa = PBXResourcesBuildPhase; 145 | buildActionMask = 2147483647; 146 | files = ( 147 | F5C080BC23FB4861003725AB /* LaunchScreen.storyboard in Resources */, 148 | F5C080B923FB4861003725AB /* Preview Assets.xcassets in Resources */, 149 | F5C080B623FB4861003725AB /* Assets.xcassets in Resources */, 150 | ); 151 | runOnlyForDeploymentPostprocessing = 0; 152 | }; 153 | /* End PBXResourcesBuildPhase section */ 154 | 155 | /* Begin PBXSourcesBuildPhase section */ 156 | F5C080A823FB485D003725AB /* Sources */ = { 157 | isa = PBXSourcesBuildPhase; 158 | buildActionMask = 2147483647; 159 | files = ( 160 | F5C080B023FB485D003725AB /* AppDelegate.swift in Sources */, 161 | F5C080B223FB485D003725AB /* SceneDelegate.swift in Sources */, 162 | F5C080C623FB494C003725AB /* StructUIViewRepresentable.swift in Sources */, 163 | F5C080C423FB48F9003725AB /* ViewModel.swift in Sources */, 164 | F5C080B423FB485D003725AB /* ContentView.swift in Sources */, 165 | F5C080C823FB49EA003725AB /* FinalClassUIViewRepresentable.swift in Sources */, 166 | ); 167 | runOnlyForDeploymentPostprocessing = 0; 168 | }; 169 | /* End PBXSourcesBuildPhase section */ 170 | 171 | /* Begin PBXVariantGroup section */ 172 | F5C080BA23FB4861003725AB /* LaunchScreen.storyboard */ = { 173 | isa = PBXVariantGroup; 174 | children = ( 175 | F5C080BB23FB4861003725AB /* Base */, 176 | ); 177 | name = LaunchScreen.storyboard; 178 | sourceTree = ""; 179 | }; 180 | /* End PBXVariantGroup section */ 181 | 182 | /* Begin XCBuildConfiguration section */ 183 | F5C080BE23FB4861003725AB /* Debug */ = { 184 | isa = XCBuildConfiguration; 185 | buildSettings = { 186 | ALWAYS_SEARCH_USER_PATHS = NO; 187 | CLANG_ANALYZER_NONNULL = YES; 188 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 189 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 190 | CLANG_CXX_LIBRARY = "libc++"; 191 | CLANG_ENABLE_MODULES = YES; 192 | CLANG_ENABLE_OBJC_ARC = YES; 193 | CLANG_ENABLE_OBJC_WEAK = YES; 194 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 195 | CLANG_WARN_BOOL_CONVERSION = YES; 196 | CLANG_WARN_COMMA = YES; 197 | CLANG_WARN_CONSTANT_CONVERSION = YES; 198 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 199 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 200 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 201 | CLANG_WARN_EMPTY_BODY = YES; 202 | CLANG_WARN_ENUM_CONVERSION = YES; 203 | CLANG_WARN_INFINITE_RECURSION = YES; 204 | CLANG_WARN_INT_CONVERSION = YES; 205 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 206 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 207 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 208 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 209 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 210 | CLANG_WARN_STRICT_PROTOTYPES = YES; 211 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 212 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 213 | CLANG_WARN_UNREACHABLE_CODE = YES; 214 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 215 | COPY_PHASE_STRIP = NO; 216 | DEBUG_INFORMATION_FORMAT = dwarf; 217 | ENABLE_STRICT_OBJC_MSGSEND = YES; 218 | ENABLE_TESTABILITY = YES; 219 | GCC_C_LANGUAGE_STANDARD = gnu11; 220 | GCC_DYNAMIC_NO_PIC = NO; 221 | GCC_NO_COMMON_BLOCKS = YES; 222 | GCC_OPTIMIZATION_LEVEL = 0; 223 | GCC_PREPROCESSOR_DEFINITIONS = ( 224 | "DEBUG=1", 225 | "$(inherited)", 226 | ); 227 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 228 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 229 | GCC_WARN_UNDECLARED_SELECTOR = YES; 230 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 231 | GCC_WARN_UNUSED_FUNCTION = YES; 232 | GCC_WARN_UNUSED_VARIABLE = YES; 233 | IPHONEOS_DEPLOYMENT_TARGET = 13.2; 234 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 235 | MTL_FAST_MATH = YES; 236 | ONLY_ACTIVE_ARCH = YES; 237 | SDKROOT = iphoneos; 238 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 239 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 240 | }; 241 | name = Debug; 242 | }; 243 | F5C080BF23FB4861003725AB /* Release */ = { 244 | isa = XCBuildConfiguration; 245 | buildSettings = { 246 | ALWAYS_SEARCH_USER_PATHS = NO; 247 | CLANG_ANALYZER_NONNULL = YES; 248 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 249 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 250 | CLANG_CXX_LIBRARY = "libc++"; 251 | CLANG_ENABLE_MODULES = YES; 252 | CLANG_ENABLE_OBJC_ARC = YES; 253 | CLANG_ENABLE_OBJC_WEAK = YES; 254 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 255 | CLANG_WARN_BOOL_CONVERSION = YES; 256 | CLANG_WARN_COMMA = YES; 257 | CLANG_WARN_CONSTANT_CONVERSION = YES; 258 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 259 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 260 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 261 | CLANG_WARN_EMPTY_BODY = YES; 262 | CLANG_WARN_ENUM_CONVERSION = YES; 263 | CLANG_WARN_INFINITE_RECURSION = YES; 264 | CLANG_WARN_INT_CONVERSION = YES; 265 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 266 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 267 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 268 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 269 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 270 | CLANG_WARN_STRICT_PROTOTYPES = YES; 271 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 272 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 273 | CLANG_WARN_UNREACHABLE_CODE = YES; 274 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 275 | COPY_PHASE_STRIP = NO; 276 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 277 | ENABLE_NS_ASSERTIONS = NO; 278 | ENABLE_STRICT_OBJC_MSGSEND = YES; 279 | GCC_C_LANGUAGE_STANDARD = gnu11; 280 | GCC_NO_COMMON_BLOCKS = YES; 281 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 282 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 283 | GCC_WARN_UNDECLARED_SELECTOR = YES; 284 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 285 | GCC_WARN_UNUSED_FUNCTION = YES; 286 | GCC_WARN_UNUSED_VARIABLE = YES; 287 | IPHONEOS_DEPLOYMENT_TARGET = 13.2; 288 | MTL_ENABLE_DEBUG_INFO = NO; 289 | MTL_FAST_MATH = YES; 290 | SDKROOT = iphoneos; 291 | SWIFT_COMPILATION_MODE = wholemodule; 292 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 293 | VALIDATE_PRODUCT = YES; 294 | }; 295 | name = Release; 296 | }; 297 | F5C080C123FB4861003725AB /* Debug */ = { 298 | isa = XCBuildConfiguration; 299 | buildSettings = { 300 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 301 | CODE_SIGN_STYLE = Automatic; 302 | DEVELOPMENT_ASSET_PATHS = "\"UIViewRepresentableBug/Preview Content\""; 303 | DEVELOPMENT_TEAM = 87X93GPQ56; 304 | ENABLE_PREVIEWS = YES; 305 | INFOPLIST_FILE = UIViewRepresentableBug/Info.plist; 306 | LD_RUNPATH_SEARCH_PATHS = ( 307 | "$(inherited)", 308 | "@executable_path/Frameworks", 309 | ); 310 | PRODUCT_BUNDLE_IDENTIFIER = jp.mediweb.UIViewRepresentableBug; 311 | PRODUCT_NAME = "$(TARGET_NAME)"; 312 | SWIFT_VERSION = 5.0; 313 | TARGETED_DEVICE_FAMILY = "1,2"; 314 | }; 315 | name = Debug; 316 | }; 317 | F5C080C223FB4861003725AB /* Release */ = { 318 | isa = XCBuildConfiguration; 319 | buildSettings = { 320 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 321 | CODE_SIGN_STYLE = Automatic; 322 | DEVELOPMENT_ASSET_PATHS = "\"UIViewRepresentableBug/Preview Content\""; 323 | DEVELOPMENT_TEAM = 87X93GPQ56; 324 | ENABLE_PREVIEWS = YES; 325 | INFOPLIST_FILE = UIViewRepresentableBug/Info.plist; 326 | LD_RUNPATH_SEARCH_PATHS = ( 327 | "$(inherited)", 328 | "@executable_path/Frameworks", 329 | ); 330 | PRODUCT_BUNDLE_IDENTIFIER = jp.mediweb.UIViewRepresentableBug; 331 | PRODUCT_NAME = "$(TARGET_NAME)"; 332 | SWIFT_VERSION = 5.0; 333 | TARGETED_DEVICE_FAMILY = "1,2"; 334 | }; 335 | name = Release; 336 | }; 337 | /* End XCBuildConfiguration section */ 338 | 339 | /* Begin XCConfigurationList section */ 340 | F5C080A723FB485D003725AB /* Build configuration list for PBXProject "UIViewRepresentableBug" */ = { 341 | isa = XCConfigurationList; 342 | buildConfigurations = ( 343 | F5C080BE23FB4861003725AB /* Debug */, 344 | F5C080BF23FB4861003725AB /* Release */, 345 | ); 346 | defaultConfigurationIsVisible = 0; 347 | defaultConfigurationName = Release; 348 | }; 349 | F5C080C023FB4861003725AB /* Build configuration list for PBXNativeTarget "UIViewRepresentableBug" */ = { 350 | isa = XCConfigurationList; 351 | buildConfigurations = ( 352 | F5C080C123FB4861003725AB /* Debug */, 353 | F5C080C223FB4861003725AB /* Release */, 354 | ); 355 | defaultConfigurationIsVisible = 0; 356 | defaultConfigurationName = Release; 357 | }; 358 | /* End XCConfigurationList section */ 359 | }; 360 | rootObject = F5C080A423FB485D003725AB /* Project object */; 361 | } 362 | -------------------------------------------------------------------------------- /UIViewRepresentableBug/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | @UIApplicationMain 4 | class AppDelegate: UIResponder, UIApplicationDelegate { 5 | 6 | 7 | 8 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 9 | // Override point for customization after application launch. 10 | return true 11 | } 12 | 13 | // MARK: UISceneSession Lifecycle 14 | 15 | func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { 16 | // Called when a new scene session is being created. 17 | // Use this method to select a configuration to create the new scene with. 18 | return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) 19 | } 20 | 21 | func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set) { 22 | // Called when the user discards a scene session. 23 | // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. 24 | // Use this method to release any resources that were specific to the discarded scenes, as they will not return. 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /UIViewRepresentableBug/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /UIViewRepresentableBug/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /UIViewRepresentableBug/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /UIViewRepresentableBug/ContentView.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | 3 | struct ContentView: View { 4 | @ObservedObject var viewModel: ViewModel = ViewModel() 5 | 6 | var body: some View { 7 | VStack { 8 | Text("Struct UIViewRepresentable").font(.title) 9 | StructUIViewRepresentable(viewModel: viewModel) 10 | Text("Final Class UIViewRepresentable").font(.title) 11 | FinalClassUIViewRepresentable(viewModel: viewModel) 12 | } 13 | } 14 | } 15 | 16 | struct ContentView_Previews: PreviewProvider { 17 | static var previews: some View { 18 | ContentView() 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /UIViewRepresentableBug/FinalClassUIViewRepresentable.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | import UIKit 3 | 4 | final class FinalClassUIViewRepresentable: UIViewRepresentable { 5 | typealias UIViewType = UILabel 6 | var viewModel: ViewModel 7 | 8 | func makeUIView(context: UIViewRepresentableContext) -> UILabel { 9 | let label = UILabel() 10 | label.font = UIFont.boldSystemFont(ofSize: 100.0) 11 | label.textAlignment = .center 12 | return label 13 | } 14 | 15 | func updateUIView(_ uiView: UILabel, context: UIViewRepresentableContext) { 16 | print("updateUIView: \(String(describing: type(of: self)))") 17 | uiView.text = "\(viewModel.count)" 18 | } 19 | 20 | init(viewModel: ViewModel) { 21 | self.viewModel = viewModel 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /UIViewRepresentableBug/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | $(PRODUCT_BUNDLE_PACKAGE_TYPE) 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UIApplicationSceneManifest 24 | 25 | UIApplicationSupportsMultipleScenes 26 | 27 | UISceneConfigurations 28 | 29 | UIWindowSceneSessionRoleApplication 30 | 31 | 32 | UISceneConfigurationName 33 | Default Configuration 34 | UISceneDelegateClassName 35 | $(PRODUCT_MODULE_NAME).SceneDelegate 36 | 37 | 38 | 39 | 40 | UILaunchStoryboardName 41 | LaunchScreen 42 | UIRequiredDeviceCapabilities 43 | 44 | armv7 45 | 46 | UISupportedInterfaceOrientations 47 | 48 | UIInterfaceOrientationPortrait 49 | UIInterfaceOrientationLandscapeLeft 50 | UIInterfaceOrientationLandscapeRight 51 | 52 | UISupportedInterfaceOrientations~ipad 53 | 54 | UIInterfaceOrientationPortrait 55 | UIInterfaceOrientationPortraitUpsideDown 56 | UIInterfaceOrientationLandscapeLeft 57 | UIInterfaceOrientationLandscapeRight 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /UIViewRepresentableBug/Preview Content/Preview Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /UIViewRepresentableBug/SceneDelegate.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | import SwiftUI 3 | 4 | class SceneDelegate: UIResponder, UIWindowSceneDelegate { 5 | 6 | var window: UIWindow? 7 | 8 | 9 | func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { 10 | // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. 11 | // If using a storyboard, the `window` property will automatically be initialized and attached to the scene. 12 | // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). 13 | 14 | // Create the SwiftUI view that provides the window contents. 15 | let contentView = ContentView() 16 | 17 | // Use a UIHostingController as window root view controller. 18 | if let windowScene = scene as? UIWindowScene { 19 | let window = UIWindow(windowScene: windowScene) 20 | window.rootViewController = UIHostingController(rootView: contentView) 21 | self.window = window 22 | window.makeKeyAndVisible() 23 | } 24 | } 25 | 26 | func sceneDidDisconnect(_ scene: UIScene) { 27 | // Called as the scene is being released by the system. 28 | // This occurs shortly after the scene enters the background, or when its session is discarded. 29 | // Release any resources associated with this scene that can be re-created the next time the scene connects. 30 | // The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead). 31 | } 32 | 33 | func sceneDidBecomeActive(_ scene: UIScene) { 34 | // Called when the scene has moved from an inactive state to an active state. 35 | // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive. 36 | } 37 | 38 | func sceneWillResignActive(_ scene: UIScene) { 39 | // Called when the scene will move from an active state to an inactive state. 40 | // This may occur due to temporary interruptions (ex. an incoming phone call). 41 | } 42 | 43 | func sceneWillEnterForeground(_ scene: UIScene) { 44 | // Called as the scene transitions from the background to the foreground. 45 | // Use this method to undo the changes made on entering the background. 46 | } 47 | 48 | func sceneDidEnterBackground(_ scene: UIScene) { 49 | // Called as the scene transitions from the foreground to the background. 50 | // Use this method to save data, release shared resources, and store enough scene-specific state information 51 | // to restore the scene back to its current state. 52 | } 53 | 54 | 55 | } 56 | -------------------------------------------------------------------------------- /UIViewRepresentableBug/StructUIViewRepresentable.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | import UIKit 3 | 4 | struct StructUIViewRepresentable: UIViewRepresentable { 5 | typealias UIViewType = UILabel 6 | var viewModel: ViewModel 7 | 8 | func makeUIView(context: UIViewRepresentableContext) -> UILabel { 9 | let label = UILabel() 10 | label.font = UIFont.boldSystemFont(ofSize: 100.0) 11 | label.textAlignment = .center 12 | return label 13 | } 14 | 15 | func updateUIView(_ uiView: UILabel, context: UIViewRepresentableContext) { 16 | print("updateUIView: \(String(describing: type(of: self)))") 17 | uiView.text = "\(viewModel.count)" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /UIViewRepresentableBug/ViewModel.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | import Combine 3 | 4 | class ViewModel: ObservableObject { 5 | var count = 0 6 | private var timer: Timer? 7 | 8 | init() { 9 | timer = Timer.scheduledTimer( 10 | timeInterval: 1.0, 11 | target: self, 12 | selector: #selector(countUp), 13 | userInfo: nil, 14 | repeats: true 15 | ) 16 | } 17 | 18 | @objc private func countUp() { 19 | objectWillChange.send() 20 | count += 1 21 | } 22 | } 23 | --------------------------------------------------------------------------------