0

I have a file called UIElements.swift which contains some extension I want to use throughout my app.

They've worked great so far. Until I created a new viewController, and I can't get any of them to work in that or any other viewControllers I make. They only work in my first viewController?

Here's the code for the extension file:

import UIKit

extension UIImage {
    //create image from UIColor, to use for buttons
    class func imageWithColor(color:UIColor?) -> UIImage! {
        let rect = CGRectMake(0.0, 0.0, 1.0, 1.0);
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
        let context = UIGraphicsGetCurrentContext();
        if let color = color {
            color.setFill()
        }
        else {
            UIColor.whiteColor().setFill()
        }
        CGContextFillRect(context, rect);
        let image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
}

Obviously I have more extensions in the file, but this is the function I was trying to call first when I noticed it didn't work.

Here is the code that doesn't work, in a file called Login_VC.swift

import UIKit

class LoginVC: UIViewController {

    override func viewDidLoad() {

        super.viewDidLoad()

        var signInButton = UIButton()
        signInButton.setTranslatesAutoresizingMaskIntoConstraints(false)
        signInButton.setTitle("SIGN IN", forState: .Normal)
        signInButton.titleLabel!.font =  UIFont(name: "BebasNeue-Bold", size: 50)
        signInButton.setBackgroundImage(UIImage.imageWithColor(UIColor.colorWithHex("#12D99E")), forState: .Normal)
        self.view.addSubview(signInButton)

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

The above code returns this following error:

'UIImage.Type' does not have a member named 'imageWithColor'

and here is a similar button in the first view controller I made called LandingPageVC.swift (this code works fine)

import UIKit

class LandingPageVC: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()


    var signUpButton: UIButton = UIButton()
    signUpButton.setTranslatesAutoresizingMaskIntoConstraints(false)
    signUpButton.setTitle("Sign Up Today", forState: .Normal)
    signUpButton.titleLabel!.font =  UIFont(name: "BebasNeue-Bold", size: 50)
    signUpButton.setBackgroundImage(UIImage.imageWithColor(UIColor.colorWithHex("#12D99E")), forState: .Normal)
    signUpButton.setBackgroundImage(UIImage.imageWithColor(UIColor.colorWithHex("#0DB07F")), forState: .Highlighted)
    signUpButton.layer.shadowColor = formulaShadowColor.CGColor
    signUpButton.layer.shadowOffset = CGSizeMake(3, 3)
    signUpButton.layer.shadowRadius = 0
    signUpButton.layer.shadowOpacity = 1.0
    signUpButton.addTarget(self, action: "signUp:", forControlEvents: .TouchUpInside)
    signUpButton.addTarget(self, action: "signUpHighlighted:", forControlEvents: .TouchDown)
    signUpButton.addTarget(self, action: "signUpReset:", forControlEvents: .TouchDragExit)
    self.view.addSubview(signUpButton)

}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

I can't see anything I've done differently in either file.

yet when I start writing UIImage.i -> it suggests imageWithColor in the first view controller, but it doesn't show up in any other file.

I didn't do anything to import this class in either file. As it's just an extension of UIImage, it should be imported with UIKit.

I also tried adding public to the class, which didn't do anything.

can anyone explain why this function works in one file, but not the other?

Kampai
  • 22,848
  • 21
  • 95
  • 95
MLyck
  • 4,959
  • 13
  • 43
  • 74

2 Answers2

1

Have you tried cleaning out your project (Command-Option-Shift-K)? Cleaning your project empties out the Xcode cache. Even better is to quit your project and clean out ~/Library/Developer/Xcode/DerivedData manually. Make sure you delete everything in this folder, as the bug could be cached in any file.

  • yeah, I tried cleaning it. However I havn't cleaned out that folder, 1 moment and I will try. Thank you so much for the suggestion! I'll get back to you in a second – MLyck Mar 03 '15 at 12:57
  • Alright, I deleted everything in the /Library/Developer/Xcode/DerivedData folder, and cleaned out the project again. Unfortunately the problem still persists. Any other ideas are very welcome :( this is incredibly frustrating. – MLyck Mar 03 '15 at 13:08
  • 1
    Can you try returning self.image from your extension and see if it works? –  Mar 03 '15 at 13:09
  • 1
    The only difference I really see between the code is that Login_VC doesn't declare the variable with an explicit type UIButton –  Mar 03 '15 at 13:14
  • 1
    Also if you try to put the extension in the same file as the class what happens? –  Mar 03 '15 at 13:16
  • 1. returning self.image in the extension causes an error, same as the one in my original post. 2. It's not just in the button, If I start typing UIImage.i (anywhere in the file), it should start autocompletion like it does in my other view controller. 3. yes, same directory, they're linked correctly. 4. Putting the extension in the file, fixes the issue. But that's obviously not what I'd like to do with a ton of extensions and classes for every single file. – MLyck Mar 03 '15 at 13:21
  • 1
    What happens when you create a brand new class and try to access the extension from there? –  Mar 03 '15 at 13:24
  • 1
    Also what happens if you change UIImage! to ? –  Mar 03 '15 at 13:25
  • Tried making a new extension class called "test" that just returns the string "WORKED", well it didn't work either. :( 1 second on the "?" – MLyck Mar 03 '15 at 13:28
  • Changed it to a "?", still nothing – MLyck Mar 03 '15 at 13:29
  • 1
    Okay well we have it narrowed down to an extension issue not a UIImage issue –  Mar 03 '15 at 13:30
  • Yeah it's all classes in the file, I can access them from one view Controller, not any other ones. Makes no sense to me :( – MLyck Mar 03 '15 at 13:31
  • Wow this is an odd bug. Can you make a new test Xcode project with an extension and see if it works there –  Mar 03 '15 at 13:33
  • Alright, made a new project. With 2 view Controllers and a swift file with the same extension class. Without doing anything else, I could use the class in both files. So I tried to intentionally mess it up a bit. I renamed some files, saved them. And this time, they both broke. But in an even weirder way, as it couldn't call anything at all. With the project I'm having problems with I can call all the normal functions of UIImage, just not the custom ones. – MLyck Mar 03 '15 at 13:43
  • 1
    This sounds like a bug. Try to file a report with Apple. In the meantime, you can try to quit Xcode or restart your computer to see if SourceKit crashes are part of the problem. –  Mar 03 '15 at 13:57
  • Unfortunately I've tried that multiple times as well :( Thank you so much for the suggestions though. – MLyck Mar 03 '15 at 14:50
0

I sent a bug-report to apple about this

Since this question has been untouched for a while not. I ended up creating a new project and copying my files over. It's working now. Still have no clue as to why it failed to work. The files are the same, same structure, same names, same targets.

MLyck
  • 4,959
  • 13
  • 43
  • 74