archive-com.com » COM » T » TECHOTOPIA.COM

Total: 1018

Choose link from "Titles, links and description words view":

Or switch to "Titles and links view".
  • Objective-C 2.0 Operator Precedence - Techotopia
    can see from the above output Objective C considers the answer to be 210 This is a direct result of operator precedence Objective C has a set of rules that tell it in which order operators should be evaluated in an expression Clearly Objective C considers the multiplication operator to be of a higher precedence than the addition operator Objective C Operator Precedence and Associativity When addressing the issue of operator precedence in some scripting and programming languages all that is generally required is a table listing the operators in order of precedence from highest to lowest Objective C has more in common with languages such as Java and C in that operators are grouped together at different precedence levels When operators from the same precedence level are found within the context of a single expression a rule as to the order in which the operators are to be evaluated is followed This rule is referred to as the associativity and differs from one group to the next The following table outlines the operator precedence groups and corresponding associativity for Objective C Operator Description Precedence Associativity access array element or message expression access object member or method invoke a method or function pointer to structure member Highest left to right sizeof type increment decrement unary plus unary minus logical NOT ones complement pointer reference address of size of object type cast right to left multiply divide modulus left to right add subtract left to right bitwise shift left bitwise shift right less than left to right less than or equal to greater than or equal to greater than left to right equality inequality left to right bitwise AND left to right bitwise XOR left to right bitwise OR left to right logical AND left to right logical OR left to

    Original URL path: http://www.techotopia.com/index.php/Objective-C_2.0_Operator_Precedence (2016-02-13)
    Open archived version from archive


  • Commenting Objective-C Code - Techotopia
    problem Now back to that old saying Don t comment bad code re write it What this phrase suggests is that if code is well written in the first place you do not need comments to explain what it does and how it does it It also suggests that if you are having to write lots of comments to explain what a section of your Objective C program does then you must have written it badly Whilst one should always strive to write good code there is absolutely nothing wrong with including comments to explain what the code does Even a well written program can be difficult to understand if it is solving a difficult problem so ignore the old programmer s adage and never hesitate to comment your Objective C code Another useful application of comments in Objective C is to comment out sections of a program Putting comment markers around sections of code ensures that they are ignored by the compiler during compilation This can be especially useful when you are debugging a program and want to try out something different but do not want to have to delete the old code until you have tested that the new code actually works Single Line Comments The mechanism for a single line comment is marked by prefixing the line with this will familiar to C or Java programmers For example This is a comment line It is for human use only and is ignored by the Objective C compiler NSString myString int i 0 This is another comment The syntax tells the compiler that everything on the same line following on from the is a comment and should be ignored This means that anything on the line before the comment marker is not ignored The advantage of this is that it enables comments to be placed at the end of a line of code For example NSString myString Welcome to Techotopia Variable containing pointer to welcome string object In the above example everything after the marker is considered a comment and therefore ignored by the Objective C compiler This provides an ideal method for placing comments on the same line of code that describe what that particular line of code does Multi line Comments For the purposes of supporting comments that extend over multiple lines Objective C borrowed some syntax from the C programming language The start and end of lines of comments are marked by the and markers respectively Everything immediately after the and before the is considered to be a comment regardless of where the markers appear on a line For example This Function adds two numbers together and returns the result of the addition int addNums num1 num2 return num1 num2 Multi line comments are particularly useful for commenting out sections of a program you no longer wish to run but do not yet wish to delete together with an explanation of when and why you have commented it out Commented out December 23 while testing

    Original URL path: http://www.techotopia.com/index.php/Commenting_Objective-C_Code (2016-02-13)
    Open archived version from archive

  • Objective-C Flow Control with if and else - Techotopia
    code to be performed when expression evaluates to true Note that the braces are only required if more than one line of code is executed after the if expression If only one line of code is listed under the if the braces are optional For example the following valid code int x 10 if x 10 x 10 Essentially if the boolean expression evaluates to 1 true then the code in the body of the statement is executed see Objective C Operators and Expressions for more details of this type of logic The body of the statement is enclosed in braces If on the other hand the expression evaluates to 0 false the code in the body of the statement is skipped For example if a decision needs to be made depending on whether one value is greater than another we would write code similar to the following int x 10 if x 9 NSLog x is greater than 9 Clearly x is indeed greater than 9 causing the message to appear in the console window Using if else Statements The next variation of the if statement allows us to also specify some code to perform if the expression in the if statement evaluates to false The syntax for this construct is as follows if boolean expression Code to be executed if expression is true else Code to be executed if expression is false Using the above syntax we can now extend our previous example to display a different message if the comparison expression evaluates to be false int x 10 if x 9 NSLog x is greater than 9 else NSLog x is less than 9 In this case the second NSLog statement would execute if the value of x was less than 9 Using if else if Statements

    Original URL path: http://www.techotopia.com/index.php/Objective-C_Flow_Control_with_if_and_else (2016-02-13)
    Open archived version from archive

  • The Objective-C switch Statement - Techotopia
    is already starting to become somewhat hard to read and also took more time to write than should really be necessary Imagine however if instead of five numbers we had to test for more Clearly an easier solution is needed and that solution is the switch statement Using the switch Statement Syntax The syntax for an Objective C switch statement is as follows switch expression case match1 statements break case match2 statements break default statements break This syntax needs a little explanation before we embark on creating a switch based version of the above if else construct In the above syntax outline expression represents either a value or an expression which returns a value This is the value against which the switch operates Using our example this would be the integer to be evaluated For each possible match a case statement is required followed by a match value Each potential match must be of the same type as the governing expression Following on from the case line are the Objective C statements that are to be executed in the event of the value matching the case match After the statements comes a break statement This statement breaks out of the switch statement Failure to provide a break statement results in every case after the matching case evaluating to true regardless of whether the match is made or not and the corresponding Objective C statements executing Finally the default section of the construct defines what should happen if none of the case statements present a match to the expression A switch Statement Example With the above information in mind we may now construct a switch statement which provides the same functionality as our previous and somewhat unwieldy if else if construct import Foundation Foundation h int main int argc const char

    Original URL path: http://www.techotopia.com/index.php/The_Objective-C_switch_Statement (2016-02-13)
    Open archived version from archive

  • Objective-C Looping - The for Statement - Techotopia
    one statement is to be executed the braces are optional though still recommended for code readability and so that you don t forget the add them if you later increase the number of statements to be performed in the loop Bringing this all together we can create a for loop to perform the task outlined in the earlier example int j 10 for int i 0 i 10 i j j NSLog j i j Objective C Loop Variable Scope A key point to note in creating loops is that any variables defined within the body of a loop are only visible to code within the loop This is concept known as scope If for example a variable myCounter is defined in a for loop that variable ceases to exist once the loop terminates variable myCounter does not yet exist for int i 0 i 10 i int myCounter 0 myCounter variable created in scope of for loop myCounter i after loop exit variable myCounter is now out of scope and ceases to exist Creating an Infinite for Loop A for loop that will execute an infinite number of times may be constructed using for syntax For example the following code sample will output Hello from Objective C until the program is manually terminated by the user or the computer is turned off or rebooted for NSLog Hello from Objective C Breaking Out of a for Loop google IOSBOX google Having created a loop it is possible that under certain conditions you might want to break out of the loop before the completion criteria have been met particularly if you have created an infinite loop One such example might involve continually checking for activity on a network socket Once activity has been detected it will be necessary to break out of the monitoring loop and perform some other task For the purpose of breaking out of a loop Objective C provides the break statement which breaks out of the current loop and resumes execution at the code directly after the loop For example int j 10 for int i 0 i 100 i j j if j 100 break NSLog j i j In the above example the loop will continue to execute until the value of j exceeds 100 at which point the loop will exit Nested for Loops So far we have looked at only a single level of for loop It is also possible to nest for loops where one for loop resides inside another for loop For example int j for int i 0 i 100 i NSLog i i i for j 0 j 10 j NSLog j i j The above example will loop 100 times displaying the value of i on each iteration In addition for each of those iterations it will loop 10 times displaying the value of j Breaking from Nested Loops An important point to be aware of when breaking out of a nested for loop is that the break

    Original URL path: http://www.techotopia.com/index.php/Objective-C_Looping_-_The_for_Statement (2016-02-13)
    Open archived version from archive

  • Objective-C Looping with do and while Statements - Techotopia
    not greater than 100 the code in the braces is executed and the loop returns to the while statement and repeats the evaluation of myCount This process repeats until the value of myCount is greater than 100 at which point the loop exits Objective C do while loops It is often helpful to think of the do while loop as an inverted while loop The while loop evaluates an expression before executing the code contained in the body of the loop If the expression evaluates to false on the first check then the code is not executed The do while loop on the other hand is provided for situations where you know that the code contained in the body of the loop will always need to be executed at least once For example you may want to keep stepping through the items in an array until a specific item is found You know that you have to at least check the first item in the array to have any hope of finding the entry you need The syntax for the do while loop is as follows do Objective C statements here while conditional expression In the do while example below the loop will continue until the value of a variable named i equals 0 int i 10 do i while i 0 Breaking from Loops As with the for loop it is also possible to exit from a while or do while loop at any time through the use of the break statement When the execution path encounters a break statement the looping will stop and execution will proceed to the code immediately following the loop In the following example the loop is coded to exit when the value of i matches the value of j int i 0 int

    Original URL path: http://www.techotopia.com/index.php/Objective-C_Looping_with_do_and_while_Statements (2016-02-13)
    Open archived version from archive

  • An Overview of Objective-C Object Oriented Programming - Techotopia
    also be defined to accept more than one argument For example to define a method that accepts both the account number and account balance we could declare it as follows void setAccount long y andBalance double x Now that we have an understanding of the structure of method declarations within the context of the class interface definition we can extend our BankAccount class accordingly interface BankAccount NSObject double accountBalance long accountNumber void setAccount long y andBalance double x void setAccountBalance double x double getAccountBalance void setAccountNumber long y long getAccountNumber void displayAccountInfo end Having defined the interface we can now move on to defining the implementation of our class Declaring an Objective C Class Implementation The next step in creating a new class in Objective C is to write the code for the methods we have already declared This is performed in the implementation section of the class definition An outline implementation is structured as follows implementation NewClassName ClassMethods end In order to implement the methods we declared in the interface section therefore we need to write the following implementation BankAccount void setAccount long y andBalance double x accountBalance x accountNumber y void setAccountBalance double x accountBalance x double getAccountBalance return accountBalance void setAccountNumber long y accountNumber y long getAccountNumber return accountNumber void displayAccountInfo NSLog Account Number ld has a balance of f accountNumber accountBalance end We are now at the point where we can write some code to work with our new BankAccount class Declaring Initializing and Releasing a Class Instance So far all we have done is define the blueprint for our class In order to do anything with this class we need to create instances of it The first step in this process is to declare a variable to store a pointer to the instance when it is created We do this as follows BankAccount account1 Having created a variable to store a reference to the class instance we can now allocate memory in preparation for initializing the class account1 BankAccount alloc In the above statement we are calling the alloc method of the BankAccount class note that alloc is a class method inherited from the parent NSObject class as opposed to an instance method created by us in the BankAccount class Having allocated memory for the class instance the next step is to initialize the instance by calling the init instance method account1 account1 init For the sake of economy of typing the above three statements are frequently rolled into a single line of code as follows BankAccount account1 BankAccount alloc init In the first step of this section we allocated memory for the creation of the class instance Good programming conventions dictate that memory allocated to a class instance should be released when the instance is no longer required Failure to do so can result in memory leaks such that the application will continue to use up system memory until none is left Those familiar with Java will be used to relying on the garbage

    Original URL path: http://www.techotopia.com/index.php/An_Overview_of_Objective-C_Object_Oriented_Programming (2016-02-13)
    Open archived version from archive

  • Writing Objective-C Class Methods - Techotopia
    of the class Since these methods are specific to the class overall as opposed to working on different instance data encapsulated in each class instance they are considered to be class methods Creating a New Class Method google ADSDAQBOX FLOW google In order to demonstrate the concept of class methods we are going to work on a simplified version of our BankAccount class and add write a class method that will count the number of instances of the BankAccount class that have been initiated To do this we are going to write a new alloc method for class called newAlloc The ingredients we need to achieve this are as follows A static variable accessible to all class instances to store the instance count A declaration for a our newAlloc class method in the interface section A declaration for a class method to return the current value of the instance count in the interface section Implementations of the two new class methods in the implementation section Some code to create instances of the class using the newAlloc method and to obtain and output the instance count Now that we know what we need to write we can begin creating our class method example The interface Section Our interface section needs to declare two class methods In An Overview of Objective C Object Oriented Programming we learned that instance methods are prefixed by the minus sign We declare class methods using the plus sign as follows in our BankAccount h file import Foundation Foundation h interface BankAccount NSObject BankAccount newAlloc int totalOpen end We now have our two class methods declared Note that since the newAlloc method is to return a pointer to a block of memory where we will be initializing an instance of a BankAccount class we must declare the return type accordingly As indicated the totalOpen method will return an integer Neither method accepts any arguments The next step is to work on the implementation of these methods The implementation Section The first item needed in the implementation section of our class is a variable to contain the current count Since we want this variable to be visible only within the scope of this file we will declare the variable as static the topic of variable scope is covered in detail in Objective C Variable Scope and Storage Class We then need to implement our two new class methods Let s start by writing the code in our BankAccount m file and then analyze what we are doing import BankAccount h static int openAccounts 0 implementation BankAccount BankAccount newAlloc openAccounts return BankAccount alloc int totalOpen return openAccounts end google IOSBOX google The first line of the implementation creates a static integer variable in which we will store the count of BankAccount instances and initializes it to 0 Within the body of the implementation we then have our newAlloc class method This method increments the current value of the openAccounts integer before calling the standard alloc class method and returning

    Original URL path: http://www.techotopia.com/index.php/Writing_Objective-C_Class_Methods (2016-02-13)
    Open archived version from archive



  •