How to make firebase list like this on swift - ios

This is what I want it to be
first I did it like this:
self.ref.child("User/CareGiver/\(CaregiverUID)/Followed").setValue([value2])
the result looks like in the picture. but when I add second data. It replaces the old one.
so I change to this
self.ref.child("User/CareGiver/\(CaregiverUID)").updateChildValues([
"Followed" : ["\(value2)"]])
it still replaces data at position[0] and never make it to position 1
how can I do
array [UID1,UID2,UID3] to firebase (not add array data at the same time so it would be like this)
-[0] UID1
-[1] UID2
-[2] UID3
without replacing another one?
ps.sorry for broken English
JSON should look likes this
{
"Name_Care" : "asdsท",
"Tel_Care" : "kknlk",
"Role" : "Care Giver",
"Followed" : [
"UID_A",
"UID_B",
"UID_C",
"UID_E"
]
}
in firebase would be like in picture

So if you want to add new values to your "followed" dictionary, you should use "setValue" function with uniq id for your new user.
Example:
self.ref.child("User/CareGiver/\(CaregiverUID)/Followed").setValue("3":"user_name")
In this case you add record user_name for key 3
I think this way can help you.

Related

How to use dynamic localization keys with SwiftUI

I'm trying to display a localized text based on an ID. I have a lot of these, so it feels more efficient to just add dynamically the ID to the string.
The string file looks like this:
"users.1.name" = "Alice"
"users.2.name" = "Bob"
"users.3.name" = "Charles"
...
If I do the following, hardcoding the ID, it works as expected and displays the associated translated key:
Text("users.1.name")
However if I do this it only displays the string:
Text("users.\(user.id).name")
// displays "users.1.name" instead of "Alice"
I've also tried:
Text(LocalizedStringKey("users.\(user.id).name"))
// displays "users.1.name" instead of "Alice"
Am I missing something or is this not possible?
You need then the following. Tested with Xcode 11.4 / iOS 13.4
Text(NSLocalizedString("users.\(id).name", comment: ""))
I'm not a SwiftUI expert but you can use:
let key:String = "users.\(user.id).name"
Text(LocalizedStringKey(key))
// OR compact
Text(LocalizedStringKey(String("users.\(user.id).name")))

Swift Firebase database overwriting

I am making a real-time messenger using Firebase. Currently, whenever I press a button I want a new message to be appended to the channel with the index of the message, but currently, whenever I press the button a new message is created that overwrites the old message. I know that setValue is usually the issue, but I really cannot tell what I'm doing wrong. What the database looks like before I add my new message. This is what it looks like after I add a new message here, and then the code I am using to add to the database.
#IBAction func sendMessageTapped(_ sender: Any) {
if messageTextField.text == "" {
print("blank")
return
} else {
// First we will update the amount of messages that the channel has.
ref.child("channels").child(channelName!).setValue(["numberOfMessages" : numberOfMessages+1 ])
numberOfMessages += 1
// after we have updated the amount of messages we will try to create a new message.
ref.child("channels").child(channelName!).child("messages").child(String(numberOfMessages)).child("message").child("content").setValue(messageTextField.text)
ref.child("channels").child(channelName!).child("messages").child(String(numberOfMessages)).child("message").child("name").setValue("Buddy")
}
}
ok, Firebase is not a traditional table based database, is a DOCUMENT based database. At the very top you have a thing called a "collection" which is just a list of "document" things. In your case, you'd have several collection things to serve as channels: "General", "TopicQ", "InterstingStuff" etc, and within them each message as a document. No need to have a document, to then list the messages within it.
Second, you don't need indexes as you're using them, make the message id an attribute of the message, because firebase support querying by field, and even then is questionable because if you make each message a document, they will have their own auto generated id's if you want.
Third, in your code you're rewriting the whole document each time, this is why you lose your previous messages, so if you keep it, you need to add a merge option:
// Update one field, creating the document if it does not exist.
db.collection("cities").document("BJ").setData([ "capital": true ], merge: true)
you probably want to do something like this. This is what I did for my app, hope this helps someone. This rootRef.childByAutoId() generates a new entry with unique id. You can use this as reference for your case.
let rootRef = Database.database().reference(withPath: "channels")
let childRef = rootRef.childByAutoId()
let values = ["Type": self.textField.text!, "message": self.textView.text!] as? [String : Any]
childRef.updateChildValues(values)

databaseRef Firebase withoud childByAutoID()

Guten Tag,
i'm uploading some text to firebase through my iOS Application, but i can't find a code, where i can give the App User not a random ID, but maybe the email for the id to upload or Name. I hope you understand what i mean.
Current Code:
let logininit : [String : AnyObject] = ["text4" : text4!, "text5" : text5!, "text6" : text6!]
let databaseRef = FIRDatabase.database().reference()
databaseRef.child("Logins").childByAutoId().setValue(logininit)
Now i want to remove this childByAutoID, and set it to, how i said, email or name of the user.
Firebase Database
EDIT
Resolved this Problem: databaseRef.child("Logins").child(The Email That You Want Here).setValue(logininit)
I think I understood what you meant, but I don't see reasons for you to do that. Usually when you have a new entry on some entity, you usually gives it a unique ID (UUID). So, I think if you use child(The Email That You Want Here), should work as you want:
databaseRef.child("Logins").child(The Email That You Want Here).setValue(logininit)

Objective-C iOS parse string according to the definition

I want create an iOS app for my school.
This App will show Week schledule and when I tap on Cell with Subject, it will show me detail info about subject...
My problem:
Our teachers use shotcuts for their names, but I want show their full name... I created the file "ucitele.h" with definitions of their names, but I don't know, how to use it 😕.
This is how that file looks:
//
// ucitele.h
//
#define Li #"RNDr. Dan---vá"
#define He #"Mgr. Ja---hl"
#define Sm #"Ing. Mich---rek"
#define Ks #"Mgr. Svat---á"
I get the shortcut of Teacher from previous view from "self.ucitel" and I maybe want compare the contents of the "self.ucitel" with definitions and set the "ucitelFull" string from the definitions? I don't know how to say it 😕.
when the content of the self.ucitel will be #"Sm", than I want parse "ucitelFull" as #"Ing. Mich---rek"
Answers in Objective-C only please
Okay, sounds like your trying to map a short identifier to a full name:
-(NSString*)fullNameFromShortName:(NSString*)short {
NSDictionary * names = #{#"Li" : #"RNDr. Dan---vá",
#"He" : #"Mgr. Ja---hl", ... };
return [names objectForKey:short];
}
Use like:
self.ucitelFull = [self fullNameFromShortName:self.ucitel];
This is a dictionary that has the short name as a key and the full name as the value.
Some further suggestions:
try using lowercase keys and comparing lowercaseString's, incase the user doesn't enter the value with the correct case.
You can move the dictionary definition into a json file and read it from your bundle, to eliminate the hardcoding

Reading strings from a text file in Objective-C

The game is similar to the quiz game. Questions are pictures and answers are strings.
Just wondering what would be the best way to read strings(answer) from the text file randomly, in order to use the string selected to pull up pictures(questions) from a set of pictures. Pictures will have the same names as all the name strings in the text file, however I can't have them repeat.
As of now I have switch statement that has multiple cases that select the picture(question) and strings(answers). Basically I don't want to keep all the strings in code in a .m file.
The question will be in a form of a picture and a text file will hold answers.
answers.txt
gta
fifa
minecraft
Questions:
gta.jpg
fifa.jpg
minecraft.jpg
so the randomizer will for example pick
answer gta
and when it does so, it should select the right pic(gta.jpg)
so at the end it will look like this:
gta.jpg
and four answers choices including the gta and the player will pick the right answer
is this clear?
Use a property list. Store the list of questions as an array of dictionaries, where each dictionary has entries for the question file name and the answer, like this:
[
{
"question" : "gta.jpg",
"answer" : "gta"
},
{
"question" : "fifa.jpg"
"answer" : "fifa"
},
//...
]
Then you can read the dictionary into memory using a convenience method:
NSArray *questions = [NSArray arrayWithContentOfFile:pathToQuestionsPList];
I think Caleb's suggestion is very good. By having an array of dictionaries, string and it's image are always kept together.
You could write a one-time parser method that would take a text file as input and generate your output plist. I'm thinking your file would be multiple lines of imageNameanswer. You'd then read the text file, use the NSString method componentsSeparatedByString to break it up into lines by line break, and then loop through the lines, again using componentsSeparatedByString: #"\i" (the tab character), this time to break it into a filename and an answer string. You turn the results into an array of dictionaries and write it out to your app's documents folder. Then just drag the result into your project.
If you wanted to get really fancy you could turn your text file parser into a command-line tool, and make it part of the build process so that when you update your text file of image names and answers, the build process automatically runs the parser on it and copies the output into the application bundle. Methinks that's a little beyond your current abilities however.
If you have a lot of questions you probably want to learn to use core data and a database. If you only have a few, then the plist or dictionary method will work.
One of my apps has a bunch of stories in a database and at the last minute, we decided to add images. Rather than messing with the database, I wrote a quick class that uses a dictionary to pair the story with the an image name.
The view controller queries the class to get the name of the image.
NSString *imageName = [EarlyReadingImageNames findStoryImage:title];
This is the full class.
//
// EarlyReadingImageNames.m
// Words
//
// Created by John Scarry on 5/20/14.
//
#import "EarlyReadingImageNames.h"
#implementation EarlyReadingImageNames
+(NSString *) findStoryImage:(NSString *)story {
NSDictionary* imageDictionary= #{ #"Alice the Worker Bee" : #"Alice",
#"Alice Learns to Fly" : #"Alice",
#"Alice Loves Her New Job" : #"Alice",
#"George Likes to Sing" : #"George",
#"George Likes to Dance" : #"George",
#"George Saves the Day" : #"George",
#"Jensen Meets Bob the Buffalo" : #"JensenBob",
#"Jensen and Bob Play in the Pond" : #"JensenBob",
#"Jensen and Bob Make a Pair of Boots" : #"JensenBob",
#"Rita Finds a New Home" : #"Rita",
#"Rita Makes a Boat" : #"Rita",
#"Rita Loves Words" : #"Rita",
#"The Rock That Looked Like a Frog" : #"Sandy",
#"The Rock and the Rainbow" : #"Sandy",
#"Sandy Makes New Friends" : #"Sandy",
#"James and the Bowl of Baseballs" : #"James",
#"James and the Garden" : #"James",
#"James Builds a Bird House" : #"James",
#"Lily Finds Eggs" : #"Lily",
#"Lily and Bessie the Cow" : #"Lily",
#"Lily Feeds the Lambs" : #"Lily",
#"Hector and Bo" : #"Hector",
#"Hector Loves Fish Socks" : #"Hector",
#"Hector Makes a Kite" : #"Hector",
#"Yoshi and Toshiko Get a New Home" : #"ToshikoYoshi",
#"Yoshi and Toshiko Go to the Library" : #"ToshikoYoshi",
#"Yoshi and Toshiko Go to the Park" : #"ToshikoYoshi",
#"Pete Loves Birds" : #"Pete",
#"Pete Meets Max" : #"Pete",
#"Pete and Max Are Best Friends" : #"Pete"
};
return [imageDictionary valueForKey:story];
}
#end

Resources