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 2 years ago.
Improve this question
I am trying keep track of previously selected row index of tableview inside sideMenu.
Here is a simple and reliable solution. Use the willSet property observer to keep track of the previous value of a variable.
var previousSelectedIndex: IndexPath?
var selectedIndex: IndexPath? {
willSet {
previousValue = selectedIndex
}
}
Use the selectedIndex variable to keep track of the current selected index which you can get from your didSelect delegate.
var previousSelectedIndex = 0
var currentSelectedIndex = 0 {
didSet {
print("===>current", currentSelectedIndex)
print("===>previous", previousSelectedIndex)
}
willSet {
previousSelectedIndex = self.currentSelectedIndex
}
explanation: didSet and willSet is property Observer in swift
didset is use to get current value of variable and will set is use to get previous value of variable .
IF you want to learn more detail about propertyObserver refer this link
https://medium.com/the-andela-way/property-observers-didset-and-willset-in-swift-4-c3730f26b1e9
Related
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 3 months ago.
Improve this question
Hello I wrote this little piece of code, but I have the impression that it is not optimal, indeed as the field variable is in get only : I can not directly change it .... but I am junior : so I would be delighted if someone has a better idea :) thank you .
let keyword = ["potatoes","garlic"]
var field: String {
var element = ""
keyword.forEach {
element += "&field=" + $0
}
return element
}
a shorter code coming from professionalswift developper :)
this would be better
let keyword = ["potatoes","garlic"]
var field: String {
return keyword.map { "&field=\($0)"}.joined()
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I created a project and I added that swipe gesture thing, my ViewController has a label when I swipe left👈 It replaces my label with random string from my array (It works ok),However when I swipe right👉 I want to see the previous string from my array the problem is that it is a random string so I can’t keep track of it, any ideas how to do it?
To keep track of all the random elements generated, create an array randoms.
var randoms = [String]()
Also create a currentIndex to keep track of the currently visible element from randoms.
var currentIndex = 0
Add all the randomly generated elements to it on swipeLeft().
func swipeLeft() {
if let randomString = arr.randomElement() {
randoms.append(randomString)
currentIndex = randoms.count - 1
label.text = randomString
}
}
Then on swipeRight(), pick the 2nd last element (if exist) from the randoms array and show on the label.
func swipeRight() {
if currentIndex > 0 {
label.text = randoms[currentIndex - 1]
currentIndex -= 1
}
}
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/
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 6 years ago.
Improve this question
Why can't I just have TodayTableViewCell instead of TodayTableViewCell.self?
private let reuseIdentifier = "Cell"
override func viewDidLoad() {
super.viewDidLoad()
tableView.registerClass(TodayTableViewCell.self, forCellReuseIdentifier: reuseIdentifier)
}
TableView's registerClass method expects an argument of type AnyObject.Type. You should pass a value of the cell's type.
From Apple's documentation:
You can use the postfix self expression to access a type as a value.
For example, SomeClass.self returns SomeClass itself, not an instance
of SomeClass
You can read more here.
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 8 years ago.
Improve this question
What I want to do is create a method in one class of my iOS app and then call that method from other classes in my app. Could someone explain exactly what I need to do to achieve this? Any help would be greatly appreciated as all my attempts so far have failed!
Thanks.
class Principle
{
var name:String = ""
func get_name() -> String
{
return self.name
}
}
class School
{
var princ:Principle = Principle()
init()
{
princ.name = "Mike"
println(princ.get_name())
}
}
class MyClass : OptionalSuperClass, OptionalProtocol1 {
var myProperty:String
var myOptionalProperty:String?
// More properties...
func doIt(a:Int) -> Int {
return a
}
var a = MyClass()
a.doIt(1)