Expected declaration error (while loop) [closed] - ios

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.

Related

What is better in this case: extension or function? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 months ago.
Improve this question
I have a ViewController where there's a logic for the "known" and "unknown" location.
At a high level it looks like this:
class MyViewController {
private var myVar: CLLocationCoordinate2D? // is updated somewhere
private var myFunc1() {
let someCond = myVar == nil // "isLocationUnknown" logic
}
private var myFunc2() {
guard let myVar == nil else { return } // "isLocationUnknown" logic
}
}
Now there's a requirement to handle the "invalid location" case.
Which means in addition to the nullability check, the CLLocationCoordinate2D check should be performed.
And I can't decide what is better ( as I don't know where to learn about Swift implementation details except of reading the sources :) ) :
Approach #1:
private func isLocationUnknown(_ location: CLLocationCoordinate2D?) -> Bool {
guard let location = location else {
return true
}
return !CLLocationCoordinate2DIsValid(location)
}
Approach #2:
private extension Optional where Wrapped == CLLocationCoordinate2D {
var isUnknown: Bool {
guard let self = self else {
return true
}
return !CLLocationCoordinate2DIsValid(self)
}
}
The criterias of comparison:
semantics: I guess #2 is more "swifty"/expressive etc
compilation time: can there be any difference (at scale)?
run-time performance: can there be any difference (at scale)?
I believe in this particular case all criterias are not important, but if this enum would be public, and called e.g. many times per second, or within multiple places in the codebase, then I'd like to be more confident when making the decision.
A class and func are reference types and stored on the heap and is therefore slower to access. Since the type Optional is an enum it is stored on the stack and will be quicker to access. But in this case it would probably not be a noticeable difference. Do which ever you feel is right.
If you want to read about memory management, here's a good article:
https://manasaprema04.medium.com/memory-management-in-swift-heap-stack-arc-6713ca8b70e1m
And here is a question about stack vs. heap memory:
Swift stack and heap understanding
EDIT:
Another thought is that your first approach takes CLLocationCoordinate2D as a parameter which creates a new copy of CLLocationCoordinate2D. So the space complexity is probably larger as well. But if that's the case, minuscule.

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.

Set an UIImage to an Imageview Swift [closed]

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")

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