I want to pass some text into the textfield but something wrong....idk why print() is work [duplicate] - swift

This question already has an answer here:
how can i put these print text into Textfield?
(1 answer)
Closed 5 years ago.
swift4 Xocode9
UNUserNotificationCenter.current().getPendingNotificationRequests {
DispatchQueue.main.async {
// Contextual closure type '() -> Void' expects 0 arguments,
// but 1 was used in closure body
let str:String = ""
self.finalresulter.text = str
self.finalresulter.text = "\($0.map{$0.content.title})"
}
print($0.map { $0.content.title},",",$0.map { $0.content.subtitle},","
,$0.map { $0.content.body},","
,$0.map { $0.trigger!})
}
please help...how to fix..

Try this:
notificatecontent.text = "\($0.map{$0.content.title})"

The issue appears to be identical to your other question. In order not to replicate existing answer I gave there, I would only mention the second part, which would look like:
UNUserNotificationCenter.current().getPendingNotificationRequests { requests in
// Textfield assignment here ...
print(requests.map { $0.content.title},",",requests.map { $0.content.subtitle},","
,requests.map { $0.content.body},","
,requests.map { $0.trigger!})
}
But, unless you explicitly want to print everything on one line, this print should be better replaced with regular for loop (or at least a forEach method):
// 1
for item in requests {
print(item.content.title, item.content.subtitle, item.content.body)
}
// 2
requests.forEach {
print($0.content.title, $0.content.subtitle, $0.content.body)
}

Related

What am I missing in my random lowercase string generator function ? Using Swift 5.5

I have a function I use to generate random strings for email addresses or passwords (for example). It was originally set like this:
static func random(length: Int) -> String {
let characters = "abcdefghijklmnopqrstuvwxyz"
return String((0..<length).map { _ in characters.randomElement()! })
}
So I changed it to this:
static func random(length: Int) -> String {
let characters = CharacterSet.lowercaseLetters
return String((0..<length).map { _ in characters.randomElement()! })
}
But I get an error saying "Value of type 'CharacterSet' has no member 'randomElement'.
I'm very new to Swift and I've done a lot of searching and so far I haven't found a good solution. I want to keep this function short and sweet. And I've been at this for a while now. Any help would be greatly appreciated! Please let me know if any more context is needed.
Edit: My question got closed because it was seen as a duplicate but, I looked at the solution page and tried to apply it to my issue and still no resolution. I'm not sure if that's because the previous answers are from 2015 and older or they were for obj-c
As I said in my comment of the possible duplicated post you can use that extension from the accepted answer to get all characters from a CharacterSet and get a randomElement from the resulting collection. Also as stated in the accepted answer some characters may not be present in the font used to display the result:
extension CharacterSet {
var array: [Character] {
var result: [Character] = []
for plane: UInt8 in 0...16 where hasMember(inPlane: plane) {
for unicode in UInt32(plane) << 16 ..< UInt32(plane + 1) << 16 {
if let uniChar = UnicodeScalar(unicode), contains(uniChar) {
result.append(Character(uniChar))
}
}
}
return result
}
}
let lowercaseLettersArray = CharacterSet.lowercaseLetters.array
let randomCharacter = lowercaseLettersArray.randomElement()! // "ᵳ"

Convert string that is all caps to only only having first letter of each word caps [duplicate]

This question already has answers here:
How to capitalize each word in a string using Swift iOS
(8 answers)
Closed 5 years ago.
I'm getting strings from a server that is all caps like:
"HELLO WORLD"
but I'm trying to make is so each word is caps on its own like:
"Hello World"
I've tried this:
extension String {
func capitalizingFirstLetter() -> String {
let first = String(characters.prefix(1)).capitalized
let other = String(characters.dropFirst()).lowercased()
return first + other
}
mutating func capitalizeFirstLetter() {
self = self.capitalizingFirstLetter()
}
}
but the result is
"Hello world"
any idea on how to do this?
Apple already did that for you:
print("HELLO WORLD".capitalized)
Documentation: https://developer.apple.com/documentation/foundation/nsstring/1416784-capitalized

Removing "Optional" in Swift 4 [duplicate]

This question already has answers here:
How do I prevent my text from displaying Optional() in the Swift interpolation?
(3 answers)
Closed 5 years ago.
That's my code
let temperature = String(describing: Int(incomeTemp.text!))
celcjuszScore.text = "\(temperature)"
print(temperature)
Whent I am pushing a button, the result of print is "Optional(32)" (When I am writing 32 in incomeTemp). I would like to have "Optional" removed and only "32" should stay.
Just unwrap it.
if let temperature = Int(incomeTemp.text!) {
celcjuszScore.text = "\(temperature)"
print(temperature)
}
Remove the optional when converting text to number: Int(incomeTemp.text!) ?? 0.
Or solve the error explicitly:
if let temperature = Int(incomeTemp.text ?? "") {
celcjuszScore.text = "\(temperature)"
} else {
celcjuszScore.text = "Invalid temperature"
}

swift 2.2 basic, if statement doesn't compile [duplicate]

This question already has an answer here:
Error "{ expected after if statement"
(1 answer)
Closed 6 years ago.
Pardon me for beginner's question, Why this code got complained about { expected after if, the braces are already there
var years = Int(edtYears.text!)
if years !=nil {
//do something
}else {
//...
}
Thanks
You need to add space between both side of condition like if years != nil { or you can also write without space but the both side if years!=nil {
var years = Int("")
if years != nil {
//do something
}else {
//...
}
Never do like this. Make sure you do optional chaining otherwise you will surely get a crash.
if let text = edtYears.text, let convertToInt = Int(text){
print("Int \(convertToInt)")
}else{
print("Cannot convert")
}

Is there anyway to make log function? [duplicate]

This question already has answers here:
Macros in Swift?
(8 answers)
Closed 6 years ago.
I want to print #file, #function and #line with one method.
I tried the below code but problem is here.
Wherever I call logm(), it always prints information of logm method itself even I declared it as #inline.
#inline(__always) func logm(items: Any...) {
if let f = #file.componentsSeparatedByString("/").last {
print("[\(f)][\(#function)][\(#line)]:", items)
} else {
print("[\(#function)][\(#line)]: ", items)
}
}
Is there anyway to implement this kind of thing? Why does #inline work as I expected?
You can pass the file, line, and function as parameters with the directives as default values:
func logm(items: Any..., file: String = #file, line: Int = #line, function: String = #function) {
if let f = file.componentsSeparatedByString("/").last {
print("[\(f)][\(function)][\(line)]:", items)
} else {
print("[\(function)][\(line)]: ", items)
}
}