2

How can I place my code to identify the device in the Extension file?

if UIDevice().userInterfaceIdiom == .pad {
        switch UIScreen.main.nativeBounds.height {
        case 2048:
            print("iPad Pro 9.7/Air")
        case 2224:
            print("iPad Pro 10.5")
        case 2732:
            print("iPad Pro 12.9")
            displayResultLabel.frame = CGRect(x: 2, y: 90, width: 370, height: 91)
            displayResultLabel.font = displayResultLabel.font.withSize(105)
        default:
            print("unknown")
        }
    }
B2Fq
  • 876
  • 1
  • 8
  • 23
  • Why do you need extension file? – Yitzchak Dec 07 '17 at 10:17
  • @Yitzchak So as not to take up space in the main file – B2Fq Dec 07 '17 at 10:20
  • Is it has to be an extension? Or it can be a class? just add a new **swift class file**, It'll automatically added to your build, put the code there in an extension OR a class.. It'll work, you can use it in a another file – Yitzchak Dec 07 '17 at 10:22
  • just refer this link:---- https://stackoverflow.com/questions/26028918/how-to-determine-the-current-iphone-device-model – V D Purohit Dec 12 '17 at 13:11

1 Answers1

2
extension THE_NAME_OF_CLASS_TO_EXTEND {

   func printDevice() {
        if UIDevice().userInterfaceIdiom == .pad {
            switch UIScreen.main.nativeBounds.height {
            case 2048:
                print("iPad Pro 9.7/Air")
            case 2224:
                print("iPad Pro 10.5")
            case 2732:
                print("iPad Pro 12.9")
                displayResultLabel.frame = CGRect(x: 2, y: 90, width: 370, height: 91)
                displayResultLabel.font = displayResultLabel.font.withSize(105)
            default:
                print("unknown")
            }
        }
    }
}

Then you could use this:

THE_INSTANCE_OF_THE_CLASS_THAT_HAS_THE_NAME_OF_CLASS_TO_EXTEND.printDevice()

So You just need to copy that code to a new file.

Replace The THE_NAME_OF_CLASS_TO_EXTEND with the name of your class

And replace the THE_INSTANCE_OF_THE_CLASS_THAT_HAS_THE_NAME_OF_CLASS_TO_EXTEND with the name of your INSTANCE of that class.. Than it'll work =]

Yitzchak
  • 3,303
  • 3
  • 30
  • 50
  • in extension file error "Use of undeclared type 'THE_NAME_OF_CLASS_TO_EXTEND'" – B2Fq Dec 07 '17 at 10:25
  • Change it to the name of YOUR class (By the way, I just edited the code.. recopy now) – Yitzchak Dec 07 '17 at 10:26
  • "displayResultLabel.frame = CGRect(x: 2, y: 90, width: 370, height: 91)" will not work from the Extension file? – B2Fq Dec 07 '17 at 10:29
  • @Yitzchak Hmm... you fixed it, but then you unfixed it. ;) So now the issue of giving a static example for a non-static method is here again (my opinion is that such method should not be a static one anyway, but that's just my opinion). – Eric Aya Dec 07 '17 at 10:29
  • No @Moritz, He has a variable in the class that he needs (displayResultLabel), In a static context he couldn't reach it. so now it's instance.. As you can see I changed the use to: THE_INSTANCE_OF_THE_CLASS_THAT_HAS_THE_NAME_OF_CLASS_TO_EXTEND – Yitzchak Dec 07 '17 at 10:31
  • @B2Fq Why do you think that? All public members of the class are available in an extension of the class. – Eric Aya Dec 07 '17 at 10:31
  • @Yitzchak Ah, yeah, I didn't notice the name change and was not seeing any instantiation. And I agree it should not be static in this case. :) – Eric Aya Dec 07 '17 at 10:32
  • @Yitzchak I do not really understand what should stand in place of "THE_NAME_OF_CLASS_TO_EXTEND" can give an example please – B2Fq Dec 07 '17 at 10:36
  • What is the name of the class that is currently holding your code (before moving to a new file)? this is the name that you should use. You extend that class to another file (not an exact definition for extension but thats your case) – Yitzchak Dec 07 '17 at 10:38
  • @Yitzchak like this "MainViewController.printDevice()"? – B2Fq Dec 07 '17 at 10:40
  • Please post the code that should call this method and I'll show you – Yitzchak Dec 07 '17 at 10:43