Swift, code style/format? [closed] - ios

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 8 years ago.
Improve this question
Now that if/for statements have enforced brackets for a code block I have been finding that short 1 line statements quickly start padding out the code. Is it acceptable to format these in a closure type fashion and present them as follows:
var physicsMarker = " "
if (eachChild.physicsBody != nil) { physicsMarker = "*" }
Or is this just going to leave those that look at my code going "What was he thinking" and instead I should stick with the more traditional:
var physicsMarker = " "
if (eachChild.physicsBody != nil) {
physicsMarker = "*"
}

I use the one line style frequently. I think it makes code cleaner and easier to read, especially if you are the only person on the project.
Another style that's similar:
if (eachChild.physicsBody != nil)
{ physicsMarker = "*" }
This calls attention to the identation and has a similar feel to python syntax.
To each his own!

Related

iOS Implement SQLite Search Query [closed]

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 days ago.
Improve this question
I'm working on a search bar within my iOS App and have a SQLite offlite db with about 300k records in it. The text name is how I'd like the search to person. I have the following code working which just does a basic search on String.
func searchList(searchQuery: String, filter: SearchFilter, limit: Int = 20, page: Int = 0) -> outputItems {
guard let db = self.db else { return [] }
var outputTable = Table(OfflineDBSchema.DBTables.outputTable.rawValue)
if !searchQuery.isEmpty {
outputTable = outputTable.filter(OfflineoutputTableColumnExpressions.name.lowercaseString.like("%\(searchQuery.lowercased())%"))
}
I'm looking for a search basic algorithm that is a little bit "smarter" which is better at finding those keywords and returning a results back with most relevant terms at the top.

compression a compound Variable with swift [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 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()
}

How can get unicode of an emoji in ios swift [closed]

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
I am new to swift programming.I am working in an app of emoji.When i click an emoji from Apple keyboard it show the unicode of that emoji.How can i get this.
Here is list of uicodes
https://apps.timwhitlock.info/emoji/tables/unicode
you can also use Edit->Emoji and drag the to the text
let str = "Ti😁 \u{1F601} tle"
use Swift Unicode escape sequence concept:
let emojiString = "\u{1F4C4}"
and if you want to get all emoji's Unicode then try this
let emojiRanges = [
0x1F601...0x1F64F,
0x2702...0x27B0,
0x1F680...0x1F6C0,
0x1F170...0x1F251
]
for range in emojiRanges {
for i in range {
var c = String(UnicodeScalar(i))
print(c)
}
}

How dynamically initialise object in Swift 3 [closed]

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

How to make a new line with a UILabel? [closed]

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 want the new number to get a new line without removing the old number ?
See the picture
Try
if let exitingText = label.text
{
label.text = exitingText + "\n" + (textField.text ?? "")
}
and set
label.numberOflines = 0
Here is the code you can use on click of button
- (IBAction)btnAppendTextClicked:(id)sender
{
NSString *oldText=_lblWithText.text;
_lblWithText.text=[NSString stringWithFormat:#"%#\n%#",oldText,_txtValues.text];
_lblWithText.numberOfLines=0;
_lblWithText.preferredMaxLayoutWidth =self.view.frame.size.width;
[_lblWithText setNeedsDisplay];
}

Resources