Set an UIImage to an Imageview Swift [closed] - ios

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Just started coding and i am running in to the following problem:
I have set up the following struct:
struct Question {
var Question: String!
var Answers: [String]!
var Answer : Int!
var Image : UIImage!
}
Connected the UIImageView as: #IBOutlet var Qimage: UIImageView!
I tried to fill the struct, and all the data is displayed correctly except the image? Example:
Questions = [Question(Question: "test?", Answers: ["1","2","3","4"], Answer: 2, Image: UIImage(contentsOfFile: "test"))
At last i am trying to fill the struct with a function and used:
Qimage.image = Questions[Qnumber].Image
Thanks in advance for the help!

Use: UIImage(named: "Test").. instead of UIImage(contentsOfFile: "Test")

Related

Doubts about Swift initializers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
Improve this question
I know this sounds like a silly question for all of you but I wanted to have more clarity on the differences of using init() or not in a structure
I meant that whether I use init() or not the parameters are still required. That's why I was wondering the difference of using init() or simple variables.
1^ Example without init()
struct ProductsImageView: View {
var image: String
var title: String
var time: Int
var isAllInOne: Bool = false
var body: some View {
}
}
2^ Example with init()
struct ProductsImageView: View {
var image: String
var title: String
var time: Int
var isAllInOne: Bool
init(image: String, title: String, time: Int, isAllInOne: Bool = false) {
self.image = image
self.title = title
self.time = time
self.isAllInOne = isAllInOne
}
var body: some View {
}
}
In both cases the various parameters will still be required when we call a structure in the code
ProductsImageView(image: "slider3", title: "Lorem", time: 60, isAllInOne: true)
Now I wanted to know when is it right to use init() and when not?
What are the differences?
Excuse me again for the stupid question but I prefer to have clear what I learn often I have some doubts and I ask you
If you don't write an init in the struct declaration, Swift will synthesize one for you. The init you wrote in example 2 is exactly the same as what Swift synthesizes in example 1.
However, the visibility of the synthesized init is always internal, even if the struct is public. So when you're creating a public struct and you want its init to be visible in other modules, you must write out its public init explicitly.

How to write Objective-C to Swift 4 below method? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Below is Objective-c Code.
I need this method in Swift, how can I write in Swift4?
I'm new to swift, please help.
-(NSMutableArray)dataArray
{
if(!_dataArray)
{
_dataArray = [NSMutableArray new];
}
return _dataArray;
}
#property(nonatomic,strong) NSMutableArray *dataArray;
The above method you used in Objective-C is to initialise the memory to dataArray when it is being used. It is generally used to minimise memory consumption.
In swift, this process is being handled by lazily instantiated properties, by putting a keyword lazy before the property. It will allocate the memory to property only when it is firstly being used.
lazy var dataArray = [String]()
Note: In swift, use swift based array rather than NSArray/ NSMutableArray
If you want some customisation to your dataArray, you can do it like:
lazy var dataArray: [String] = {
var temp = [String]()
temp.append("John Doe")
return temp
}()
You can refer the link: http://mikebuss.com/2014/06/22/lazy-initialization-swift/

Error in Swift 3 for calculate with String and Integer [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
This is a really simple program : Calculate Cat years
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var txtOldOfCat: UITextField!
#IBOutlet weak var lblOldOfCat: UILabel!
#IBOutlet weak var imgCat: UIImageView!
#IBAction func btnSubmit(_ sender: Any) {
print(txtOldOfCat.text!)
let catAge = Int(txtOldOfCat.text!)! * 7
print(catAge)
lblOldOfCat.text = String(catAge)
}
}
In the log console, if enter 3 to submit I can read that :
3
21
(11db)
The program breakpoint in Thread 1: breakpoint 1.1 in this line :
{ ...
lblOldOfCat.text = String(catAge)
}
I double check and the link is good betweem my main storyboard and ViewController class.
I really not understand is really easy...
Thank you
If your click on line number it's add a "blue flag' in the margin. This blue flag is a breakpoint in XCode so this is why my program stop at this line. You just need to re-click on the line in blue to delete this break point.

Expected declaration error (while loop) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am in an iOS course and came across this error while making my app:
import Foundation
import UIKit
class RootsCateogry1: ViewController {
#IBOutlet weak var roots1Label: UILabel!
var rootsWeek1 = ["acro", "micro"]
var rootsWeek1Meaning = ["Air", "Small"]
var roots1Show = []
var temp = rootsWeek1.count // error here
var p = 0
var i = 0
while(i<temp){ // error here
roots1Show.append(rootsWeek1temp)
temp++
}
// ...
}
Screenshot
temp is a computed property, not a compile-time constant. You either need to override its getter or place it in your init or viewDidLoad:
var temp: Int {
return rootsWeek1.count
}
or:
override func viewDidLoad() {
super.viewDidLoad()
var temp = rootsWeek1.count
}
Your while loop must go in a function, it cannot exist at the class level. Consider moving that to your viewDidLoad as well. You are also not declaring a variable named rootsWeek1temp before adding it to roots1Show, so the compiler won't know what to append to the array if the object does not exist.

UIImageView: Fatal error: Array index out of range [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I'm trying to repeatedly display images, but I'm getting an out of range error.
Here's my code:
#IBOutlet weak var im: UIImageView!
var images = ["page003.png","page004.png","page005.png","page006.png"]
var indice = 0
#IBAction func n(sender: AnyObject) {
indice++
if indice == images.count
{
indice == 0
}
im.image = UIImage(named: (images[indice]))
}
Replace indice == 0 with indice = 0.

Resources