Difference between revisions of "Objective-C"

From Wikicliki
Jump to: navigation, search
(Basics)
Line 19: Line 19:
 
* bool (true/false)
 
* bool (true/false)
 
=== Initialisation ===
 
=== Initialisation ===
int myInteger;
+
* int myInteger;
int myInteger = 42;
+
* int myInteger = 42;
int myInteger, myInteger2, myInteger3;
+
* int myInteger, myInteger2, myInteger3;
 
=== Operators ===
 
=== Operators ===
operators - as normally expected except there is also "modulo"
+
* operators - as normally expected except there is also "modulo"
% - result will be the remainder from the integer division of the 1st by the 2nd
+
* % - result will be the remainder from the integer division of the 1st by the 2nd
(only for int or long, insert some caveat here about floats and doubles and using [http://www.cplusplus.com/reference/cmath/fmod/ fmod])
+
* (only for int or long, insert some caveat here about floats and doubles and using [http://www.cplusplus.com/reference/cmath/fmod/ fmod])
eg: int moduloResult = a % b;
+
* eg: int moduloResult = a % b;
 
=== NSLog ===
 
=== NSLog ===
 +
String Formatters for the trace/nslog:
 +
 +
NSLog(@" a %% b = %i", c)
 +
Prints: c = b % a;
 +
 +
<pre>int myNum = 7;
 +
NSString *myString = @"Dog";
 +
NSLog(@"The number is %i and the string is %@.", myNum,
 +
    myString);</pre>
 +
 +
If you want a % sign you have to put in %%.
 +
 +
<pre>NSLog(@"%@", [NSNumber numberWithInt:i]);
 +
 +
%@ Object
 +
%d, %i signed int
 +
%u    unsigned int
 +
%f    float/double
 +
%1.2f to controll number of decimals
 +
%x, %X hexadecimal int
 +
%o    octal int
 +
%zu    size_t
 +
%p    pointer
 +
%e    float/double (in scientific notation)
 +
%g    float/double (as %f or %e, depending on value)
 +
%s    C string (bytes)
 +
%S    C string (unichar)
 +
%.*s  Pascal string (requires two arguments, pass pstr[0] as the first, pstr+1 as the second)
 +
%c    character
 +
%C    unichar
 +
%lld  long long
 +
%llu  unsigned long long
 +
%Lf    long double
 +
</pre>
  
 
== See also ==
 
== See also ==

Revision as of 09:23, 21 February 2013

Objective-C is a superset of C language and it is called objective-c because it is a object-oriented language.

What one needs in order to program in Objective-C

  • Source Code Editor (Xcode)
  • Compiler (Xcode)
  • Program to design the interface (Interface Builder - which is now built into Xcode)
  • Debuggers (Instruments)

Get Xcode

Basics

Data types in Objective-C

  • int (integer)
  • unsigned int
  • float (floating point number)
  • double (double precision)
  • char (a, b, c)
  • string ("a string")
  • bool (true/false)

Initialisation

  • int myInteger;
  • int myInteger = 42;
  • int myInteger, myInteger2, myInteger3;

Operators

  • operators - as normally expected except there is also "modulo"
  •  % - result will be the remainder from the integer division of the 1st by the 2nd
  • (only for int or long, insert some caveat here about floats and doubles and using fmod)
  • eg: int moduloResult = a % b;

NSLog

String Formatters for the trace/nslog:

NSLog(@" a %% b = %i", c) Prints: c = b % a;

int myNum = 7;
NSString *myString = @"Dog";
NSLog(@"The number is %i and the string is %@.", myNum,
    myString);

If you want a % sign you have to put in %%.

NSLog(@"%@", [NSNumber numberWithInt:i]);

%@ Object
%d, %i signed int
%u     unsigned int
%f     float/double
%1.2f to controll number of decimals
%x, %X hexadecimal int
%o     octal int
%zu    size_t
%p     pointer
%e     float/double (in scientific notation)
%g     float/double (as %f or %e, depending on value)
%s     C string (bytes)
%S     C string (unichar)
%.*s   Pascal string (requires two arguments, pass pstr[0] as the first, pstr+1 as the second)
%c     character
%C     unichar
%lld   long long
%llu   unsigned long long
%Lf    long double

See also