Summary of iOS primary errors and warnings

Source: Internet
Author: User
Tags parse error

Summary of iOS primary errors and warnings

This is an article suitable for beginners to learn and summarizes some very elementary errors and warnings.

Error Message 1. error: 'xxx' undeclared (first use in this function)

Not Defined yet (used for the first time in this function ).

Note: If a variable is not defined before use, this error occurs. In oc and C, you must define the variable before using it.

This error often occurs when you forget to define variables. However, after getting used to it, this kind of error will rarely appear. On the contrary, this error is often caused by misspelling of the variable name, that is, the variable name used is inconsistent with the defined variable name.

Example:

-(Void) test {int count = 0; return conut + 1; // the variable count is inconsistent with conut. }

Correct syntax:

-(Void) test {int count = 0; return count + 1; // change the variable name count to the same one .}
2. error: parse error before 'xxx' token

A parsing error occurred before 'xxx.

Note: This error occurs only when a low-level error occurs. Parsing errors mean that the program statements cannot be parsed using the oc syntax. Take a closer look at the cause of the error and you will surely find the parts that do not conform to the syntax.

Example:

-(Void) test {NSString * str = @ "This is test" // The semicolon NSLog (str) is missing here );}
3. error: invalid preprocessing directive # xxx

Keyword # xxx is incorrect.

Note: This error message is displayed when a spelling error occurs for the # include, # import and other keywords starting. In Xcode, the string followed by # Will change color, which is very prone to errors.

Example:

# Improt
  
   
// # Incorrect import spelling
  
4. error: xxx. h: No Such file or directory

The file or directory named xxx. h does not exist.

Note: This information is displayed when the file specified in # include, # import does not exist. The most likely cause is that the file name is incorrect. Check the file name.
If this error occurs, check the actual file. It is possible that the file retrieval path is not specified correctly. As long as the file is included in the project folder, there is no problem.

Example:

# Import "appcontrler. h" // The appcontrler. h file does not exist. // The correct one is: # import AppContrller. h
5. error: Undefined symbols: 'xxx'

The symbol 'xxx' is not defined.
Note: This error occurs not only during compilation, but also during connection. This information is displayed when a nonexistent class or function is used for connection.
The most common error is that the function name is incorrect. When calling C functions, the compilation will pass even if the name is incorrect. However, this error occurs during connection.
Other possible reasons are that the frameworks or libraries other than Cocoa are not included in the project. All required libraries or frameworks must be included in the project.

6. Expression is not assignable

Error cause: OC does not allow you to directly modify the members of the struct attribute of an object.
_ Btn is an object, and frame is a struct.
The object and struct are different. The struct is in C language and can define many attributes, but cannot define methods. The object can define attributes and define methods, is a typical object-oriented syntax.
How to change the members of the struct attribute in an object:
Solution 1: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwcmUgY2xhc3M9 "brush: java;"> // Since the struct attribute member in the object cannot be directly modified // retrieve the struct CGRect frame = _ btn first. frame; // modify the structure frame. origin. y-= 10; // assign the modified struct value back to _ btn. frame = frame;

Solution 2:

// Obtain the y value CGFloat y = _ btn first. frame. origin. y; // modify y value y-= 10; // reset the y value of _ btn. Other attributes and _ btn remain unchanged _ btn. frame = CGRectMake (_ btn. frame. origin. x, y, _ btn. frame. size. width, _ btn. frame. size. height );
7. Property 'tag' not found on object of type' _ strong id'

Error cause: Point syntax cannot be used for id type
Solution 1:

// Use the get method to obtain the tag value NSInteger I = [sender tag];

Solution 2:

// Convert the id to UIButton * button = (UIButton *) sender; // you can use the dot syntax to obtain the tag. the compiler is stupid and will only obtain the tag based on the current type, to determine whether this syntax can be used. Generally, the method of the corresponding type can be used if it is strongly converted to the corresponding type. NSInteger I = button. tag;
8. linker command failed with exit code 1 (use-v to see invocation)

The error message indicates that the @ package permission is accessed using underlines.

The member attributes modified by @ package can only be accessed within the same framework. Otherwise, link erro is triggered.
@ Private instance variables can only be accessed by declared classes
@ Protected instance variables can be accessed by declared classes and subclasses.
@ Public instance variables can be accessed by any class.

9.-[UITableViewController loadView] loaded the "2-view-3" nib but didn't get a UITableView

The reason is: The UITableViewController cannot load UITableView because it will load the UIView in stroyboard.
Solution: Change the UIView in stroyboard to UITableView.

10. Cannot assign to 'self 'outside of method in the init family

Cause: only values can be assigned to self in the init method. Xcode determines whether the value is an init method rule. The method returns an id and the name starts with init + uppercase letter + others. Example:-(id) initWithXXX;

Warning

The warning must be fixed and ignored. However, it is quite comfortable to delete all warnings, so we should try to modify them as much as possible.

1. warning: 'xxx' may not respond to 'yyy'

Class 'xxx' does not have the method 'yyy' declaration.

Note: When a method in a class is called, the class declaration does not contain this information. The first possible cause is that the method name is incorrect. Check the method name carefully to make sure it is correct.

Example:

NSString * str; str = [NSString stringWithForatm: @ "% d", 10]; // The method name is incorrect. // Correct: NSString * str; str = [NSString stringWithFormat: @ "% d", 10];

In addition, when a class calls a method defined by itself, if the method is appended to the class declaration, no problem will occur. If the actual call is in front of the method definition, this warning message is also displayed. This is because the compiler checks the method definition in sequence from the beginning of the file. With this feature, if you do not want other classes to call methods, you do not need to append them to the class declaration.

Example:
Warning:

-(Void) methodA {[self methodB]; // The definition of methodB is later}-(void) methodB {}

No warning:

-(Void) methodB {}-(void) methodA {[self methodB]; // There is no warning when methodB is defined}

What will happen if you ignore this warning? First, compilation and connection are successful, so the application can be started. However, when the application is actually running here, it will check whether the called method is actually defined in the class. If not defined, an exception is thrown; otherwise, the Operation passes normally. Therefore, if you have already defined this method in the class, you can deliberately ignore this warning.

2. warning: unused variable 'xxx'

The variable 'xxx' is not used.

Note: This information is displayed when the variable has been defined but is not used at one time. It is often said that the variable used in the past is no longer used after modification, but the definition is still saved. In this case, you only need to delete the definition of the variable. You can do this without deleting it.

In addition, this warning message is displayed when the defined variable name is inconsistent with the used variable name.

Example:

-(Void) test {int a, B; // B is not used a = 5; return a;} // correct:-(void) test {int; // Delete the definition of B a = 5; return ;}
3. warning: local declaration of 'xxx' hides instance variable

The local variable 'xxx' overwrites the instance variable (that is, the same name ).

Note: When the variable name defined in the method has the same name as a variable of the instance variable, this warning message is displayed. Because of the same name, all users will not be able to access it. In this case, the external instance variables cannot be accessed. Modify the variable names of one party.

Example:

@ Interface MyObject: NSObject {int count;} @ end @ implementation MyObject-(void) updateCount :( int) count {// The parameter name is the same as the instance variable name .}

Correct:

@ Interface MyObject: NSObject {int count;} @ end @ implementation MyObject-(void) updateCount :( int) num {// modify the parameter name, make it different from the instance variable name .}
4. warning: incomplete implementation of class 'xxx'

Warning: method definition for 'yyy' not found
The code for the class 'xxx' is not completed.
The definition of method 'yyy' is not found.
Note: This warning message is displayed when code execution is not written for a method in the class declaration. After a warning occurs, the Execution Code should be completed. If you feel that this method is not needed, you can delete the definition of this method in the class declaration file. In addition, if the method name in the actual code is inconsistent with the defined method name, this warning message is also displayed.

5. warning: control reaches end of non-void function

The return value is not set for a function of the non-void type.

Note: When a method or function requires a return value, no warning message is displayed when any return value is set. In methods other than void, you must return a specific value. If no return value is required, change the return value type of the method to void.

Conversely, if the return value is set to the void type, and a value is returned in a function or method, "'Return 'with a value is displayed, in function returning void (Return Value in void function) "warning.

Example:

-(Int) test :( int) count {count ++; // The integer value needs to be returned without returning any value} // the correct one is:-(int) test :( int) count {return count ++ ;}
6. warning: passing argument n of 'xxx' assignment from distinct Objective-C type

The Nth parameter of method 'xxx' is different from the type of Objective-C.

Note: When a parameter is passed to the method 'xxx', this warning is displayed when the parameter object passed is different from the parameter type declared in the method. For example, if the declared NSEnumerator type is passed in as NSString type, this warning is displayed.
The most likely cause is that there are many parameters for the method, and the order is wrong during the setting. When you use methods with many parameters and the warning message is displayed, check the Parameter order carefully.
In addition, you can use this warning when defining a method. If you want to specify the passed parameter as a specific class, define a clear type for the parameter. If any object can be used, it is defined as the id type. The Parameter definition contains the information that the class designer gives to the user.

Example:

Int value = 3; NSString * str; str = [NSString stringWithFormat: "% d", value]; // The stringWithFormat: parameter cannot be a C string. // Correct: int value = 3; NSString * str; str = [NSString stringWithFormat: "@ % d", value]; // append the @ symbol before the parameter string

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.