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.)
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 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 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 4 years ago.
Improve this question
Is it possible to initiate object which is codable. So that I can add values into variables.
Ex: let obj = MyClass() for NSObject class.
Something like this.
Please help me.
Thanks in advance.
Yes, it is possible:
struct Person: Codable {
let name: String
let age: Int
}
let p = Person(name: "Robert", age: 30)
print(p.name)
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 5 years ago.
Improve this question
this method was writing with swift 3 in swift 4 I get error and I don't know where is the error
var CURRENT_USER: User? {
if let currentUser = Auth.auth().currentUser {
return currentUser
}
return nil
}
The error might have something to do with the optional type since it states that:
Cannot convert return expression of type 'User' to return type 'User?'
A good place to start would be to return an optional value by changing your code to
var CURRENT_USER: User? {
return Auth.auth().currentUser
}
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 6 years ago.
Improve this question
I am trying to parse PDF files and I've nearly got the code working. The only thing I can't seem to figure out is to translate the following Objective C code, into Swift. I need to call my own written function to register it as a callback.
The Objective-C code is:
CGPDFOperatorTableSetCallback(operatorTable, "q", &op_q);
and the function is
static void op_q(CGPDFScannerRef s, void *info) {
// Do whatever you have to do in here
// info is whatever you passed to CGPDFScannerCreate
}
What would the Swift equivalents be?
You don't need to create your own op_q function to use as a function pointer if you don't want to. Use Swift's closure syntax:
CGPDFOperatorTableSetCallback(operatorTable, ("q" as NSString).UTF8String) { (s, info) -> Void in
// ...
}
Here ("q" as NSString).UTF8String gives you an UsafePointer<Int8> which acts as a const char * bridged to Swift from C.
If you wanted to use a function pointer, it might look like this:
func op_q(s: CGPDFScannerRef, _ info: UnsafeMutablePointer<Void>) {
// ...
}