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 trial3 Answers
Damien Watson
27,419 PointsHi Shaun,
Hard coded as he is using it is I believe the reference to strings or data (ie numbers) being used inside the body of your code that are not 'dynamic' in the sense that you can define in one place and use in many. A good practice is to define your strings and numbers into variables that describe what they are.
A good example of this with numbers is also referred to as 'magic numbers' for when numbers appear in your code that you may or may not know what they are.
#define TAG_FIRST_NAME 2
NSString *logInButtonText = @"SIGN UP";
In your code, it is much better to have :
UILabel *firstnameLabel = (UILabel*)[cell.contentView viewWithTag:TAG_FIRST_NAME];
firstnameLabel.text = logInButtonText;
// over say
UILabel *firstnameLabel = (UILabel*)[cell.contentView viewWithTag:2];
firstnameLabel.text = @"SIGN UP";
Shaun Kelly
5,648 PointsWhat does it mean when your making a method dynamic? I understand that when you call the method you can set the value of the string but can you explain what does it mean when you make the method "Dynamic". And also what are parameters ?
-(void) createLabelWithString:(NSString)text {
NSString *string = text; }
Damien Watson
27,419 PointsHey Shaun, it would be best to break the above into a new question in the forum as questions can only have one correct answer, and answering these helps other students. That said, I will have a crack at answering it.
Making a method dynamic mostly refers to what it does or returns. If you have a method that does the same thing, or returns the same thing then it's not dynamic. If you can pass in something and get a result returned then this would be dynamic.
A property is a variable that you want the class to store. This can be available to other classes or locked to just be available inside the class.
// A property of class 'ExampleClass' is 'randomNum'.
// Dynamic method 'getRandomFromZeroTo' takes a user defined variable and returns a result.
@interface ExampleClass ()
@property int randomNum;
@end;
@implementation ExampleClass
-(void)viewDidLoad {
self.randomNum = [self.getRandomFromZeroTo:100];
}
-(int)getRandomFromZeroTo:(int)maxNum {
return arc4random() % maxNum;
}
Shaun Kelly
5,648 PointsShaun Kelly
5,648 PointsOkay i think i understand... So when we hard code them we are manually setting a value for each object or whatever it may be. But when we are not hard coding we are setting the value with a variable that we can use over and over again. ?
Damien Watson
27,419 PointsDamien Watson
27,419 PointsYou got it, I think this matches their 'DRY' policy of not repeating yourself, also making the code easier to read in most circumstances. The other thing being that you can set up your 'defaults' at the top of the page and change them in one place.