-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using Previews #41
Comments
anyone? someone? help :) |
Is the |
Thanks for the reply. I tried that but get a "User' is not a member type of class 'Twift.Twift'". I also did the import struct Twift.User, but get the same error. |
If you delete Not sure if you can try to extend it and try if that works, or, the way I did it...store it in UserDefaults when fetching real data and use that for some offline tests :) |
When I take off Twift I get the "Missing argument for parameter 'from' in call insert 'from: <#Decoder#>, '". Can you expand upon using UserDefaults? I am wanting to be able to see the preview live for UI modifications and just pass it some random data to create it. |
Since you need to use mock data to have it in the preview, and as it is easier to actually have real data there, you can log in once, save user data, and then in the preview fetch that data through userDefaults. I made this to encode/decode stuff for userData. Just replace MODEL and KEY with proper stuff you are trying to encode/decode :) Hope this helps! extension UserDefaults {
static func encodeITEM(item: MODEL) {
Task {
let encoder = JSONEncoder()
if let data = try? encoder.encode(item) {
UserDefaults.standard.set(data, forKey: KEY)
}
}
}
static func decodeITEM() -> MODEL? {
let decoder = JSONDecoder()
if let data = UserDefaults.standard.data(forKey: KEY),
var decodedMODEL = try? decoder.decode(MODEL.self, from: data) {
return decodedMODEL
}
return nil
}
} |
Thanks will give this a whirl! |
As @roblack pointed out, Twift’s models don’t have initialisers besides decoding from JSON payloads. I originally designed it this way thinking that you'd only need to instantiate models for Tweets and related objects (such as Polls), and created specific models for those instantiations ( |
I have generated the data itself, but feeding that back into a User model so that way I can see what the userRow looks like instead of having to make changes then push device has proved cumbersome for me. All I want is an Array of Users (Twift) but I can’t seem to get this to work. What would you do in this situation? |
So if I have an array of User Data from .getFollowing that I am returning, how does this fit into the Model/Key piece? |
If you have that data, save it in UserDefaults when fetched. Then, in the preview, or on the next app launch, just use that data instead. |
or...and I am just spitballing here...how about you get that JSON and save it in a file? Then decode that instead?! That has to work! |
Thank! I'll give this a shot! Appreciate the guidance! |
Yeah I wouldn't use UserDefaults for this; a JSON file that you decode is probably the right approach (which is what I do for other apps, for example Zeitgeist). But if it feels more natural to use the model(s) directly I'm open to changing Twift to enable this if there's enough demand! |
Thanks everyone, got it working! |
Good morning,
Am I missing something here? I am trying to feed a User array into my preview, but for the life of me I cannot get this thing to work. Below is a snippet, the error is "Type Twift has no member User", however this a nearly exact copy of the results received from the api call. Thoughts.
struct FollowingView_Previews: PreviewProvider {
static var previews: some View {
let users: [User] = [Twift.User(id: "1445771133337235470", name: "Oppenheimer", username: "OppenheimerFilm", createdAt: Date(), protected: false, withheld: WithheldInformation, location: "nowhere", pinnedTweetId: "", url: "", description: "", verified: "", entities: "", profileImageUrl: "", publicMetrics: "")]
FollowingView(users: users)
}
}
The text was updated successfully, but these errors were encountered: