Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Derek Medlin
6,151 PointsRibbit Code Challenge - Signing Up New Users Part 2 : Task 1 of 4
TASK: "In our view controller below we want to sign up new users for our app. In the signup method, start by capturing the username and password from two UITextField properties: 'usernameField' and 'passwordField'. Store the values in the properties 'username' and 'password'."
MY CODE:
#import "SignupViewController.h"
#import <Parse/Parse.h>
@implementation SignupViewController
- (IBAction)signup:(id)sender {
NSString *username = self.usernameField.text;
NSString *password = self.passwordField.text;
}
@end
ALSO TRIED (incase he was just testing us):
#import "SignupViewController.h"
#import <Parse/Parse.h>
@implementation SignupViewController
- (IBAction)signup:(id)sender {
NSString *username = [self.usernameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *password = [self.passwordField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
@end
I know it must be something small that I'm missing but I've redone it several times and I can't seem to find what is wrong. This should be easy lol.
9 Answers
Ben Jakuben
Treehouse TeacherAh, I just realized what is wrong here. username and password are properties of the view controller, so you need to set them that way, not declare them as new NSString variables.
Chris Bull
56 PointsFor anyone who still gets stuck on this, and doesn't understand what the comments above mean, here is the answer: self.username = self.usernameField.text; self.password = self.passwordField.text;
Luke Schoen
3,317 PointsThanks for the feedback in this post. I was really struggling to work this one out. I agree with Ben in that providing hints and answers in the actual challenges destroys the value of the challenge and prior achievements altogether. The challenges and points system motivates me to perform my own research through the documentation. I try different approaches to solve the problem, and in the process I consolidate my learning. When my options are exhausted I search the internet and Teamtreehouse.com forums, creating my own new discussion as a last resort.
Derek Medlin
6,151 PointsYeah this site is great and I agree with Ben as well. We don't need answers spoon fed to us, if we can't think for ourselves then how are we going to be able to design an app in the first place.
robert cioffi
3,466 PointsWhats the difference between saying this:
self.username = self.usernameField.text;
self.password = self.passwordField.text;
and
username = [self.usernameField.text self];
password = [self.passwordField.text self];
I'm a little confused to what the self value does in some of the various ways we've written code
Ben Jakuben
Treehouse TeacherThis has to do with "variable scope," which defines how and when the variables will be available. As properties of the class, variables are available anywhere in the class. The self keyword refers to the current instance of a class, so we use it to reference class properties and methods. Without self, then the variable must be declared locally somewhere in the implementation file. It could be a local variable that lives within the current method, a parameter that's passed into it, or perhaps a global variable in the implementation file that's available anywhere within the file.
The takeaway here is that we use self any time we need to reference something for the class that is declared in its header file. In this code challenge, username and password are already declared in the header file as properties of this class.
J Kim
15,218 PointsI understand different contexts give us larger scope of understanding. However, I do think clear instructions would not have hurt. When things don't seem too purposeful and vague I think it could become rather misleading. Just my two cents..
Derek Saunders
5,167 PointsPaul almost, instead of using UIViewController remember to use self.
Dan Boyle
2,978 PointsI'm a little confused as well with this challenge. It's my understanding that you create the UITextField properties to capture the data entered by the user. Those are stored in usernameField and passwordField.
Why would you create two more properties called username and password?
Would those look like this?
@property (nonatomic, weak) NSString *username; @property (nonatomic, weak) NSString *password;
Or am I just going way off course here?
Ben Jakuben
Treehouse TeacherThe properties are already created for you in this challenge. You simply need to set them from the UITextField properties.
Benjamin McMahan
6,679 PointsI really think it would have been helpful to explain this one a bit better and include the header file for SignupViewController. If that were available, as it would be in the development environment, the challenge would have made a lot more sense. I think most of us were attempting exactly what the original poster detailed - NSString *username = self.usernameField.text;
Dan Boyle
2,978 PointsHi Ben... I got through the code challenge. I was just curious... is that how you would set them. And if so, why would you want to do it that way if you've already create UITextField properties for usernameField and passwordField?
Any benefit?
Ben Jakuben
Treehouse TeacherGood question. For convenience, really. You need the IBOutlet properties, but the NSString ones are for convenience. If you are using the values from the UITextFields in different places then creating properties can be more convenient than getting the values each time. You could accomplish the same thing with local variables, but it would just depend on how and where they are being used.
Part of the reason we used properties in this challenge is to make testing easier on our part since we can access the properties from our validation engine. :)
oscar gonzalez
7,072 PointsMy answer its great that Ben improve us to do our best in this challenge in my opinion im very new in this world of IOS and this helo me a lot to learn more Ben always make stack LOL because sometimes i dont have the best answer for his challenge but for this quiz the answer is this
self.username = self.usernameField.text; self.password = self.passwordField.text;
Derek Medlin
6,151 PointsDerek Medlin
6,151 PointsOh sorry, I never put my actual problem (it was early lol). I tried both of the above code and it fails. Am I missing something? Because I would almost bet money it's right or I'm just blind. Sorry for not being clear.
Derek Medlin
6,151 PointsDerek Medlin
6,151 PointsSorry I see you've updated your answer. I replied from email which had your original post. Ok thanks.
Masao Kitamura
1,783 PointsMasao Kitamura
1,783 PointsBen - why is this different than the tutorial? In the tutorial, you don't make username and password properties of the view controller (ie. you don't need "self" like you do in the code challenge). Which is more correct?
Ben Jakuben
Treehouse TeacherBen Jakuben
Treehouse TeacherWell, the reason here actually has to do with the internals of our Code Challenge engine. We can check properties in ways that we can't check local variables, which is how it is in the actual app. :)
Also, I like to make the code challenges slightly different than the actual code for a few reasons:
Anyhow - I hope it doesn't cause too much confusion. I'm certainly open to feedback about the code challenges, so if it's causing too much trouble I'll look for a way to improve them. :)
Masao Kitamura
1,783 PointsMasao Kitamura
1,783 PointsI agree with your points, and the code challenges are definitely very helpful.
However, it would be nice if there was a "show answer" option, to handle the cases when the student has exhausted all their attempted answers.
Otherwise, for me, I just copy/paste the question into Google and pray! :) (and if this fails, I would have learned more from just seeing the answer, rather than skipping the question all together)
Ben Jakuben
Treehouse TeacherBen Jakuben
Treehouse TeacherWe've discussed the merits of providing answers. If you email help@teamtreehouse.com you can ask for an answer. But we want to avoid that as much as possible to make passing the code challenges mean something more than just Googling the answer. And we want students to post problems in here so that everyone can help each other, because in the discussion and extended teaching everyone really cements their learning.
We do need to take feedback about the challenges into account and adjust as necessary. We have also talked about adding hints that increase in helpfulness, but that's just a lower priority at the moment.
Derek Medlin
6,151 PointsDerek Medlin
6,151 PointsI think it's great how it is now, I just made a stupid mistake that and it blows my mind that I didn't see it. It clearly said PROPERTY and i read right through it, not paying attention because I was cramming. You learn the most from mistakes and this code challenge, IMO, was clear enough. I just kept missing it because I had that "temporary blindness" that we all get from time to time.Thanks again for the amazing tutorials.
Paul Cisneros
1,332 PointsPaul Cisneros
1,332 PointsSo...?
UIViewController *username = self.usernameField.text; UIViewController *password = self.passwordField.text;
This one's pretty hard to crack.
Ben Jakuben
Treehouse TeacherBen Jakuben
Treehouse TeacherDerek gave you a good response below. In your code you are creating two new variables (
usernameandpassword) that are UIViewControllers. These properties are already defined in the current view controller and can be accessed usingself, just like you are accessing theusernameFieldandpasswordFieldproperties.onurtekin
7,346 Pointsonurtekin
7,346 PointsHow do we know that
usernameandpasswordare properties of view controller? Where are they defined exactly?