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/
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 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
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
I am getting the below error. My app is crashing for this reason.
Could not cast value of type '__NSSingleEntryDictionaryI' (0x1015f8210) to 'NSMutableDictionary'
All I am doing is :
var tempDict = self.arrayData.object(at: indexPath.row) as!
NSMutableDictionary
I have checked the others answers on stack but unfortunately they were not helpful for me.
Can anybody suggest me why this is happening?
Any help would be higgle appreciated!!
Simply do not use mutable Foundation collection types (NSMutable..) at all in Swift. They are not related to the Swift counterparts and you cannot cast an collection object to NSMutable...
Declare arrayData as native Swift array of dictionaries
var arrayData = [[String:Any]]()
and change the line to get the dictionary to
var tempDict = self.arrayData[indexPath.row]
Less code, no type cast and tempDict is mutable with the var keyword.
Dictionary denotes with {} and Array denotes with [] // In printed response you may have array with ()
So, your tempDict part is Array of Dictionary...You have to parse it like
var tempDict = self.arrayData.object(at: indexPath.row) as! [[String : Any]]
although please not use force unwrap .. either use if let or guard statement
if let tempDict = self.arrayData.object(at: indexPath.row) as! [[String : Any]]
{
// do something
}
else
{
// catch the error
}
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 6 years ago.
Improve this question
I use below line of code to allocate an Object(Suppose my Object name is Car) dynamically.
[self initliazieObject:[Car class]]
- (id)initliazieObject:(Class)model{
id record = [[model alloc] init];
return record;
}
How I can do this in swift 3.
Exactly as in Objective-C. Try this in a playground:
class Car : NSObject {}
func factory(type:NSObject.Type) -> NSObject {
return type.init()
}
let c = factory(type:Car.self)
print(type(of:c)) // Car
(We can get fancy and do clever things with generics or Self to specify the type of the returned object more precisely, but my goal in this code is simply to do things the dumb way, just like Objective-C.)
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 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.