Swinject / SwinjectStoryboard

Swinject extension for automatic dependency injection via Storyboard
MIT License
268 stars 141 forks source link

'@objc' class method in extension of subclass of 'SwinjectStoryboard' requires iOS 13.0.0 #173

Open whendon opened 2 years ago

whendon commented 2 years ago

macOS Monterey 12.4 (21F79)

Xcode Version 13.2.1 (13C100)

inside my podfile Target:

  platform :ios, '10.0'
  pod 'Swinject', '~> 2.6.2'
  pod 'SwinjectStoryboard'

Do I have to target iOS 13 to use this version of Swinject?

whendon commented 2 years ago

Update: tried targeting ios 13 and it did not resolve the issue.

I also tried updating Swinject to 2.7.1, which did not resolve the issue. I tried Swinject 2.8.2, which did nothing.

I tried specifying SwinjectStoryboard 2.2.0, 2.2.1, and 2.2.2.

import SwinjectStoryboard
import Foundation

extension SwinjectStoryboard {
    @objc class func setup() { // THE ERROR IS ON THIS LINE

        defaultContainer.register(JsonClient.self) { _ in
            let mobileToken = preferences.string(forKey: "mobileAuthToken") ?? ""
            return JsonClient(mobileToken: mobileToken, baseUrl:BaseUrl )
        }

        defaultContainer.storyboardInitCompleted(SignInViewController.self) { r, c in
            c.client = r.resolve(JsonClient.self)!
        }
davilinho commented 2 years ago

I've same issue, anybody has a solution?

import SwinjectStoryboard

extension SwinjectStoryboard {
    @objc class func setup() { <--- IS NEVER CALLED SINCE iOS 13!!
...
Sega-Zero commented 2 years ago

for those who struggles with setup method in XCode 14, Either turn off dead strip option, or use this workaround:

__attribute__((constructor)) static void swinjectStoryboardSetupEntry(void);

@interface SwinjectStoryboard (SetUp)

@end

@implementation SwinjectStoryboard (SetUp)

// will never get called from outside, but allows to keep swinjectStoryboardSetupEntry away from stripping
- (void) avoidDeadStripping {
    swinjectStoryboardSetupEntry();
}

@end

__attribute__((constructor)) static void swinjectStoryboardSetupEntry(void) {
    if ([SwinjectStoryboard conformsToProtocol:@protocol(SwinjectStoryboardProtocol)] &&
        [SwinjectStoryboard respondsToSelector:@selector(setup)]) {
        [SwinjectStoryboard performSelector:@selector(setup)];
    }
}
BarredEwe commented 2 years ago

Could you try this code? I think, it is important - using static func (not @objc func).

extension SwinjectStoryboard {
    static func setup() { // <- Static here, instead of @objc
        // ...
    }
}

If it works, i create PR for fixing docs.

whendon commented 2 years ago

I have already removed SwinjectStoryboard from my app - I will add it back in on Monday and attempt your change.

sowoumar commented 1 year ago

Could you try this code? I think, it is important - using static func (not @objc func).

extension SwinjectStoryboard {
    static func setup() { // <- Static here, instead of @objc
        // ...
    }
}

If it works, i create PR for fixing docs.

I use Xcode 14, but I still have the same problem. Should I remove SwinjectStoryboard in my project?

JHeisecke commented 1 year ago

in my case is never entering the library SwinjectStoryboard, like it doesn't exists in my project