Objective C Flashcards

1
Q

Class

A
for representing objects
It's defined by 2 files:
class.h      (=interface file)
a 
class.m  (=implementation file)

To represent an object in Objective C, we first need a Class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

OOP

A

Object Oriented Programming

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Variable

A

most basic yet important concept in programming
int x = 10;
If the variable consists of two words, it should start with a lower case for the first word and a upper case for the second

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Variable types

A

integer int x = 1;
char grade = ‘ ‘;
NSString *carBrand = @”Toyota”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

NSString variable data type

A

NextStep string
variable must have asterix (*) before name
NSString *name = @”John”

NSStrings are one of the most useful and commonly used data type in programming.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Constructors

A
  • (id)initWithName:(NSString *)newName gender:(NSString *)newGender age:(int) newAge;

objeví se v souboru *.m

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Methods

A

The functions we have introduced earlier, i.e. getStudentCategory, initWithName are know as methods. A method is a block of code that performs a specific task.

Method Arguments
A method can have no arguments like in the case of
- (NSString *)getStudentCategory;
A method can also have arguments like in the case of
- (id)initWithName:(NSString *)newName gender:(NSString *)newGender age:(int) newAge;

Note that the data type for each argument must be declared (in parenthesis).
- instance method
\+ class method: A class
method is one that performs some operation on the class itself, such as creating a new instance
of the class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Interface/Implementation

A

each method implemented in the *.m file, there should be a header declaration in the .h file

@interface class_name: NSObject { attributes}
@property …vlastnosti_atributu..

@end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Return Type of a method

A

The return type is the data type returned from the method. For example, the return type in the below method is NSString.
- (NSString *)getStudentCategory;

The return type below is id.
- (id)initWithName:(NSString *)newName gender:(NSString *)newGender age:(int) newAge;

If the method does not return any value, the return type is void like in the below example.
- (void)dealloc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Managing Multiple Objects

A
We can store objects in a NSMutableDictonary.
It allows us easy access to: 
a) insert new objects, 
b) delete existing objects, 
c) update existing objects 
d) and view existing objects

Data in a NSMutableDictionary is stored in key-value pairs.

Summary of NSMutableDictionary Operations

Hence in short, we store an object into the NSMutableDictionary using
setObject: forKey:

eg. [studentStore setObject:jane forKey:@”123”];

and retrieve it with objectForKey:
”“eg. Student *temp = [studentStore objectForKey:@”123”];

To remove an object, we have to use the removeObjectForKey method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

If-Else Statements

A

if (score >= 50)
{NSLog(@”Passed”);}
else
{NSLog(@”failed”);}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Format specifiers In NSLog

A
Many format specifiers work with NSLog. If we have a floating-point number, we can use %f to print out 
a floating-point number, decimal point and all.
Other format specifiers are:
%i - int (same as %d)
%lf - double
%c - char
%s - string  (also %@ - string)
%x - hexadecimal
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Evaluating conditions

A

Evaluating Conditions

Other evaluation conditions can also be
equal to, ==
not equal to, !=
more than, >
more than or equal to >=
less than, <
less than or equal to,  69 && score < 80)
NSLog(@”B grade”);

Eg. if score is less than 50 or absentWithoutExcuse is true

if (score < 50 || absentWithoutExcuse)
NSLog(@”Fail”);

Note that absentWithoutExcuse in this case is a boolean variable. A boolean value has only YES (true) or NO (false) values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Nested If-Else

A

In a If-Else block, there can be further nested If-Else blocks!

There can in fact be infinite nested If-Else blocks though a maximum of 4 levels is recommended as this makes your code unreadable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

If-Else-If

A

You can also extend your If-Else to If-Else-If-Else-If-Else…
Again, you can have infinite If-Else-If…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

NSString Comparisons

A

To compare two NSStrings, we can use the isEqualToString method of the NSString class.

NSString Length

To check the length of a NSString, we use the length method of the NSString class
“NSString rangeOfString

To check if a certain text exists in a long NSString, we can use the rangeOfString.
To check if a certain text exists in a long NSString, and not consider the case, (eg. ABC is the same as abc), we add the options:NSCaseInsensitiveSearch argument.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Common filename extensions

A
Extension * Meaning
.c C *language source file
.cc, .cpp *C++ language source file
.h *Header file
.m *Objective-C source file
.mm *Objective-C++ source file
.pl *Perl source file
.o *Object (compiled) file
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Autorelease pool

A

autorelease pool is a mechanism that allows the system to efficiently manage the memory your application uses as it creates new objects.
@autoreleasepool {
statements
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

import

A

import says to import or include the information from that file into the program, exactly as if the contents of the file were typed into the program at that point.

20
Q

main

A

main is a special name that indicates precisely where the program is to begin execution. The reserved word int that precedes main specifies the type of value main returns, which is an integer

int main (int argc, const char * argv[])

21
Q

newline character

A

\n

is very similar in concept to the carriage return key on a typewriter

22
Q

Choosing Names

A
  • must begin with a letter or underscore ( _ ), and they can be followed by any combination of letters (uppercase or lowercase), underscores, or the digits 0 through 9
  • class names begin with an uppercase letter, even though it’s not required
  • To aid readability, capital letters are used inside names to indicate the start of a new word
23
Q

The @interface Section

A

When you define a new class, you have to tell the Objective-C compiler where the class came from. That is, you have to name its parent class. Next, you need to define the type of operations, or methods, that can be used when working with objects from this class. And, as you learn in a later chapter, you also list items known as properties in this special section of the program called the @interface section.

@interface NewClassName: ParentClassName
propertyAndMethodDeclarations;
@end

24
Q

The @implementation Section

A
the @implementation section contains the actual code for the methods you declared in the @interface section. You have to specify what type of data is to be stored in the objects of this class. That is, you have to describe the data that members of the class will contain.
These members are called the instance variables. Just as a point of terminology, you say that
you declare the methods in the @interface section and that you define them (that is, give the
actual code) in the @implementation section.
@implementation NewClassName
{
memberDeclarations;
}
methodDefinitions ;
@end
25
Q

ARC

A

Automatic Reference Counting,
In the past, iOS programmers were responsible for telling the system when they were done
using an object that they allocated by sending the object a release message. That was done
in accordance with a memory management system known as manual reference counting. As of
Xcode 4.2, programmers no longer have to worry about this and can rely on the system to take
care of releasing memory as necessary.

26
Q

setters

A

methods that set the values of instance variables.
The setters don’t return a value, because their purpose is to take an argument and to set the corresponding instance variable to the value of that argument. No value needs to be returned in that case.

27
Q

getters

A

methods used to retrieve the values of instance variables.
the purpose of the getter is to “get” the value of an instance variable stored in an object and to send it back to the program. To do that, the getter must return the value of the instance variable using the return statement.

28
Q

accessor

A

Collectively, setters and getters are also referred So.
Synthesize accessor method

@implementation Fraction
@synthesize numerator, denominator;

-(void) print
{
NSLog (@"%i/%i", numerator, denominator);
}
-(double) convertToNum
{
if (denominator != 0)
return (double) numerator / denominator;
else
return NAN;
}
@end
29
Q

data encapsulation

A

the idea that you can’t directly set or get the value of an instance variable from outside of the methods written for the class, but instead have to write setter and getter methods to do so is the principle of data encapsulation. So, you have to use methods to access this data that is normally hidden to the “outside world.”

30
Q

Data types

A

Type Constant Examples NSLog chars
char ‘a’ , ‘\n’ %c
short int — %hi , %hx , %ho
unsigned short int — %hu , %hx , %ho
int 12 , -97 , 0xFFE0, 0177 %i , %x , %o, %d
unsigned int 12u , 100U , 0XFFu %u , %x , %o
long int 12L , -2001 , 0xffffL %li , %lx , %lo
unsigned long int 12UL , 100ul , 0xffeeUL %lu , %lx , %lo
long long int 0xe5e5e5e5LL , 500ll %lli , %llx , &llo
unsigned long long int 12ull , 0xffeeULL %llu , %llx , %llo
float 12.34f , 3.1e-5f , 0x1.5p10 , 0x1P-1 %f , %e , %g , %a
double 12.34 , 3.1e-5 , 0x.1p3 %f , %e , %g , %a
long double 12.34L , 3.1e-5l %Lf , $Le , %Lg
id nil %p

31
Q

Arithmetic expressions

A

arithmetic oper: + - * / %(modulus)

32
Q

Type Cast Operator

A

The type cast operator is often used to coerce an object that is a generic id type into an object of a particular class.
f2 = (float) i2 / 100; // type cast operator
The type cast operator has the effect of converting the value of the variable i2 to type float for purposes of evaluating the expression. In no way does this operator permanently affect the value of the variable i2 ; it is a unary operator that behaves like other unary operators. Just as the expression -a has no permanent effect on the value of a , neither does the expression
( float ) a .
The type cast operator has a higher precedence than all the arithmetic operators except the unary minus and unary plus. Of course, if necessary, you can always use parentheses in an
expression to force the terms to be evaluated in any desired order.

33
Q

Assignment operators

A

=, +=, -=, /=

a /= b + c eq. a = a / (b + c)

34
Q

looping capabilities

A

The for statement
The while statement
The do statement
A For loop is better suited for fixed number of repetitions. A While loop is better suited for unknown number of repetitions.
Do statement test the condition at the end of loop.
The break statement leaves actual loop (if nested, it switch to the loop one step higher).
The continue statement skips to the end of loop without breaking the loop.

35
Q

Relational Operators

A
Operator Meaning Example
== Equal to count == 10
!= Not equal to flag != DONE
< Less than a < b
 Greater than points > POINT_MAX
>= Greater than or equal to j >= 0
36
Q

Making decisions

A

The if statement
The switch statement
The conditional operator

37
Q

Conditional operator

A

Perhaps the most unusual operator in the Objective-C language is one called the conditional
operator. Unlike all other operators in Objective-C—which are either unary or binary operators—
the conditional operator is a ternary operator; that is, it takes three operands. The two
symbols used to denote this operator are the question mark ( ? ) and the colon ( : ). The first
operand is placed before the ? , the second between the ? and the : , and the third after the : .
The general format of the conditional expression is shown here:
condition ? expression1 : expression2
In this syntax, condition is an expression, usually a relational expression, that the Objective-C
system evaluates first whenever it encounters the conditional operator. If the result of the
evaluation of condition is true (that is, nonzero), expression1 is evaluated and the result of
the evaluation becomes the result of the operation. If condition evaluates false (that is, zero),
expression2 is evaluated and its result becomes the result of the operation.
s = ( x < 0 ) ? -1 : x * x;
sign = ( number < 0 ) ? -1 : (( number == 0 ) ? 0 : 1);

A non-ANSI extension to the syntax of the conditional operator, which is supported by Xcode,
is shown here:
condition ?: expression
In this syntax, condition is evaluated, and if true, then the value of the operation is
condition. If condition is false, then the value of the operation is expression.
result = index ?: -1;

38
Q

Boolean variable

A

A couple of built-in features in Objective-C make working with Boolean variables a little easier.
One is the special type BOOL , which can be used to declare variables that will contain either
a true or a false value. The other is the built-in values YES and NO . Using these predefined
values in your programs can make them easier to write and read.

39
Q

Switch statement

A
switch ( expression )
{
case value1 :
program statement
program statement
...
break;
case value2 :
program statement
program statement
...
break;
...
case valuen :
program statement
program statement
...
break;
default:
program statement
program statement
...
break;
}
The expression enclosed within parentheses is successively compared against the values
value1 , value2 , ..., valuen , which must be simple constants or constant expressions. If a case
is found whose value is equal to the value of expression , the program statements that follow
the case are executed. Note that when more than one such program statement is included, they
do not have to be enclosed within braces.
The special optional case called default is executed if the value of expression does not
match any of the case values.
40
Q

Compound relational tests

A

The Objective-C language provides the mechanisms necessary to perform compound relational tests. A compound relational test is simply one or more simple relational tests joined by either the logical AND or the logical OR operator. These operators are represented by the character pairs && and || (two vertical bar characters), respectively.

if ( grade >= 70 && grade <= 79 )
++grades_70_to_79;

The && operator has lower precedence than
any arithmetic or relational operator but higher precedence than the || operator.
The compound operators can be used to form extremely complex expressions in Objective-C.
The Objective-C language grants the programmer the ultimate flexibility in forming expressions, but this flexibility is a capability that programmers often abuse. Simpler expressions are almost always easier to read and debug.

41
Q

if statement

A

if ( expression )

program statement

42
Q

NAN

A

Not A Number

43
Q

if-else construct

A

if ( expression )
program statement 1
else
program statement 2

can be nested
if ( expression 1 )
program statement 1
else
if ( expression 2 )
program statement 2
else
program statement 3
44
Q

else-if construct

A
if ( expression 1 )
program statement 1
else if ( expression 2 )
program statement 2
else
program statement 3
45
Q

Outlet

A

Instance variables which are pointers to other objects.

45
Q

scanf formatting

A

scanf (“ %c”, &c);
it’s best to put a space before the %c in the scanf format string (as in “ %c” ). This causes scanf to “skip” over any so-called whitespace characters (for example, newlines, returns, tabs, line feeds) in the input. Omitting that space can cause scanf to read in a character that you don’t expect. Although that might not be a problem in this example, it’s good to keep this in mind when working on other examples in this chapter (including the exercises) whenever you want to read a single character.

46
Q

Actions

A

Methods that can be triggered by user interface objects.