Since I have opened my project with xcode 12 I am getting the following issue:
import UIKit
class PersonViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let imageView = UIImageView()
let carImage = UIImage(named: "carImg")
imageView.image = carImage
}
}
The error I am getting is: "No exact matches in reference to instance method 'image"
The compiler is basically not recognising that UIImageView has a property called "image" or that UIKit is not imported.
If I create a new project this compiles just fine, so I am guessing it is some sort of setting that was fine in xcode11 but not fine in xcode12.
Note: This is an Objective C project with a few classes in Swift.
Note: Restarted my mac, did clean project and removed Derived Data
Are you missing an import of UIKit at the top of your file?
import UIKit
class PersonViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let imageView = UIImageView()
let carImage = UIImage(named: "carImg")
imageView.image = carImage
}
}
class PersonViewController: UIViewController {
#IBOutlet weak var imageView: UIImageView! //This line is important and must not be excluded
Related
I placed a folder with images into my project as Folder Reference. Now I can see it in my Package in Contents/Resources/Images/foo.jpg
I try to populate an NSImage like this:
#IBOutlet var imageView: NSImageView!
override func viewDidLoad() {
super.viewDidLoad()
imageView.image = NSImage (named: "Images/foo.jpg")
}
But no image is shown when the app is loaded. I am using interface builder not XIB. Does it make a difference? If I add the images as Group I can use named:"foo.jpg" without any issue.
I understand now the inner workings. This is my solution:
#IBOutlet var imageView: NSImageView!
override func viewDidLoad() {
super.viewDidLoad()
let image = Bundle.main.path(forResource: "foo", ofType: "jpg", inDirectory: "Images")
imageView.image = NSImage (contentsOfFile: image)
}
The reference files are not named in my bundle so they are never found. With the full path it works well.
import UIKit
import CoreImage
class ViewController: UIViewController {
#IBOutlet weak var personPic: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
personPic.image = UIImage(named: "stupidsonny")
detect()
}
func detect() {
guard let personciImage = CIImage(image: personPic.image!) else { //This line has the error
return
}
Either the personPic outlet isn't connected, or the "stupidsonny" image isn't getting found and loaded. It's hard to tell which since they're both being accessed on the same line.
To help narrow it down, you could put a ! where you load the image:
personPic.image = UIImage(named: "stupidsonny")!
No matter what program I run in xCode (as long as there is an #IBOutlet in the View Controller), I get the error fatal error: unexpectedly found nil while unwrapping an Optional value
In this case, my code is a simple image slideshow:
#IBOutlet weak var imageView: UIImageView!
override func viewDidAppear(animated: Bool) {
var imagesNames = ["image-3.jpeg","image-4.jpeg","image-5.jpeg","image-6.jpeg","image-7.jpeg"]
var images = [UIImage]()
for i in 0..<imagesNames.count{
images.append(UIImage(named: imagesNames[i])!)
}
imageView.animationImages = images
imageView.animationDuration = 0.05
imageView.startAnimating()
}
I'm not sure if I used viewDidAppear() correctly but it doesn't work if the code is in a viewDidLoad() as well. And yes, my #IBOutlet is connected in the storyboard with the little gray dot next to it filled in.
I have tried redownloading xCode. Should I try again?
Thanks
e
Maybe there is an unwanted outlet referenced in the storyboard...
It happens when a View has a referenced outlet that is missing in the uiviewcontroller code.
Make sure that all referenced outlets are linked to your UIViewController :
The only thing I can think of is if the force unwrapping of UIImage is finding nil for one of your images. To double check that it's not funny business on your end you should safely unwrap the image (which is good practice anyway). Try this and see if you're still getting the error:
override func viewDidAppear(animated: Bool) {
var imagesNames = ["image-3.jpeg","image-4.jpeg","image-5.jpeg","image-6.jpeg","image-7.jpeg"]
var images = [UIImage]()
for i in 0..<imagesNames.count {
guard let image = UIImage(named: imagesNames[i]) else {
print("\(imagesNames[i]) not found!"); continue
}
images.append(image)
}
imageView.animationImages = images
imageView.animationDuration = 0.05
imageView.startAnimating()
}
Try remove the unconnected outlet. probably you have remove variables in viewcontroller file but forgot to remove the connected outlet
I am trying to self-learn OSX application development so I can make up all of my own bad habits 8).
Probably extraneous information
I have a trial app that works successfully - it resizes itself based on input from the user via a slider.
The key piece of code that does this is in one View controller ...
class JunkViewController2: NSViewController {
var myY: CGFloat!
#IBOutlet weak var mySlider: NSSlider!
#IBOutlet weak var myView: NSView!
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
self.preferredContentSize = NSMakeSize(self.view.frame.width, 83)
}
#IBAction func mySlider(sender: NSSlider) {
let mySplitViewController = self.childViewControllers[0] as! JunkSplitViewController
switch mySlider.intValue {
case 3:
myY = 140.0
mySplitViewController.splitViewItems[2].collapsed = false
mySplitViewController.splitViewItems[1].collapsed = false
mySplitViewController.showSubview(2)
mySplitViewController.showSubview(1)
mySplitViewController.showSubview(0)
case 2:
myY = 110.0
mySplitViewController.splitViewItems[2].collapsed = true
mySplitViewController.splitViewItems[1].collapsed = false
mySplitViewController.hideSubview(2)
mySplitViewController.showSubview(1)
mySplitViewController.showSubview(0)
default:
myY = 80.0
mySplitViewController.splitViewItems[2].collapsed = true
mySplitViewController.splitViewItems[1].collapsed = true
mySplitViewController.hideSubview(2)
mySplitViewController.hideSubview(1)
mySplitViewController.showSubview(0)
}
mySplitViewController.preferredContentSize = NSMakeSize(self.view.frame.width, myY - 50 + 3)
self.preferredContentSize = NSMakeSize(self.view.frame.width, myY + 3)
}
}
More pertinent information
In what is working, above, on the story board I have three duplicate ViewControllers connected to a SplitView controller. I do a bunch of what feels like belts and suspenders work to make sure that everything gets resized properly - but the key part (I think) is the .collapsed property.
I am now trying to accomplish the same thing, using a completely different method - dynamically adding / removing split view items. This should allow me to have only one of the small ViewControllers on my story board, and then instantiate it as needed.
Following that idea, here is my SplitViewController ...
class JunkSplitViewController: NSSplitViewController {
#IBOutlet weak var mySplitView: NSSplitView!
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
//mySplitView.adjustSubviews()
}
func makeChild() -> SmallViewController {
let mySmallGroup = NSStoryboard(name: "Main", bundle: nil).instantiateControllerWithIdentifier("smallVwCtl")
self.addSplitViewItem(mySmallGroup as! NSSplitViewItem)
return mySmallGroup as! SmallViewController
}
}
The main view controller invokes the makeChild function.
class JunkViewController: NSViewController {
#IBOutlet weak var mySlider: NSSlider!
#IBOutlet weak var myView: NSView!
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
self.preferredContentSize = NSMakeSize(self.view.frame.width, 83)
}
#IBAction func mySlider(sender: NSSlider) {
let mySplitViewController = self.childViewControllers[0] as! JunkSplitViewController
while mySlider.intValue.toIntMax() > mySplitViewController.splitViewItems.count.toIntMax() {
mySplitViewController.makeChild()
}
while mySlider.intValue.toIntMax() < mySplitViewController.splitViewItems.count.toIntMax(){
mySplitViewController.splitViewItems.removeLast()
}
}
}
I get an error at the self.addSplitViewItem(mySmallGroup as! NSSplitViewItem) line of JunkSplitViewController ... "Could not cast value of type Scratch2.SmallViewController to NSSplitViewItem"
I've tried a handful of combinations (forcing mySmallGroup, 'self.addSplitViewItem(mySmallGroup as! SmallViewController)`, etc.) Everything leads to a similar error, either at compile or run time.
I cannot find any documentation on SplitViewItem.
So the question - what will work as input to addSplitViewItem and still successfully connect a new instance of SmallViewController?
And gratefully accept any comments/feedback on the methodology
I hate it when I find my answer minutes after posting a question ...
Based on info I found here ...
func makeChild() -> SmallViewController {
let mySmallGroup = NSStoryboard(name: "Main", bundle: nil).instantiateControllerWithIdentifier("smallVwCtl") as! SmallViewController
self.addSplitViewItem(NSSplitViewItem(viewController: mySmallGroup))
return mySmallGroup
}
... but I'd still like to hear any feedback on methodology. Thanks.
hi im new to SO so i hope i make everything right.... :)
i started some Days ago iOS programming and no i have a problem that drives me crazy!
Here is the code of the class FactBook:
import Foundation
import UIKit
public class FactBook {
var randomNumberForAll: Int!
let factsArray = [
"Ants stretch when they wake up in the morning.",
"Ostriches can run faster than horses.",
"Olympic gold medals are actually made mostly of silver.",
"You are born with 300 bones; by the time you are an adult you will have 206.",
"It takes about 8 minutes for light from the Sun to reach Earth.",
"Some bamboo plants can grow almost a meter in just one day.",
"The state of Florida is bigger than England.",
"Some penguins can leap 2-3 meters out of the water.",
"On average, it takes 66 days to form a new habit.",
"Mammoths still walked the earth when the Great Pyramid was being built.",
"You can finish the entire FunFacts iOS app course in the time it takes to set up the emulator for the Android course"
]
let imagesArray = [
UIImage(named: "ants.jpg"),
UIImage(named: "ostriches.jpg"),
UIImage(named: "olymp.jpg"),
UIImage(named: "bones.jpg"),
UIImage(named: "light.jpg"),
UIImage(named: "bamboo.jpg"),
UIImage(named: "florida.jpg"),
UIImage(named: "penguins.jpg"),
UIImage(named: "habit.jpg"),
UIImage(named: "mammoth.jpg")
]
func randomFact() ->String {
//Count
var arrayCount = UInt32(factsArray.count)
//Random Number from count
var unsignedRandomNumber = arc4random_uniform(arrayCount)
//Random Number as Int
var randomNumber = Int(unsignedRandomNumber)
self.randomNumberForAll = randomNumber
return factsArray[randomNumber]
}
func randomImage() -> UIImage {
var imageRandom = randomNumberForAll
return imagesArray[imageRandom]!
}
}
Here is the ViewController:
import UIKit
class ViewController: UIViewController {
//Lable, Structs etc.----------------------------------NEU----------------------------
#IBOutlet weak var funFactLable: UILabel!
#IBOutlet weak var funFactButton: UIButton!
#IBOutlet weak var imageView: UIImageView!
let colorWheel = ColorWheel()
var counter = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//Loads first Label Text
funFactLable.text = "An interestig Fun Fact comes with one push!"
funFactButton.setTitle("For the first Fun Fact - Push!", forState: UIControlState.Normal)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//ACTIONS-------------------------------------------------------------------------------------
#IBAction func showFunFact() {
var randomColor = colorWheel.randomColor()
var randomImage = FactBook.randomImage()
var randomFact = FactBook.randomFact()
counter++
//BackgroundColor change
view.backgroundColor = randomColor
//Button-TextColor change
funFactButton.tintColor = randomColor
//Button-Title = Counter + Text
funFactButton.setTitle("\(counter). Fun Fact", forState: UIControlState.Normal)
//fadeIn fadeOut FactLable
self.funFactLable.fadeOut(completion: {
(finished: Bool) -> Void in
self.funFactLable.text = randomFact
self.funFactLable.fadeIn()
})
//fadeIn fadeOut ImageView
self.imageView.fadeOut(completion: {
(finished: Bool) -> Void in
self.imageView.image = randomImage
self.imageView.fadeIn()
})
}
}
so the problem is, i cant call the functions (randomFact and randomImage) in the ViewController.
But always the Error Missing argument for parameter #1 in call comes up.
When i change the class factbook to struct i can call the funcĀ“s but then the self.randomNumberForAll doesnt work.
What i just want is to use the randomnumber from the func randomFact() in the func randomImage()....
Thank you if u can help me :) !!!!
I hope this isn't a case of the blind trying to lead the blind as I'm new to swift, but I took this course too recently and was playing around with the code similarly to you. Seems like loading in the Factbook like you did Colorwheel should eliminate your missing parameter error. Unlike the colorwheel though, maybe it has to be a variable instead of a constant, so that you can store and change your randomFactForAll:
let colorWheel = ColorWheel()
var factBook = FactBook()
little late, hope this helps
Need to add keyword "class" to your functions randomFact() and randomImage() to make them as class functions.
Please check the following question for similar issue.