InObjective-CMedium and manyStringThe usage is the content to be introduced in this article. It mainly includes the interchange of strings and time, the link of strings, and so on. Let's take a look at the details. Let's take a lookObjective-CMediumStringAnd date conversion.
1. Convert string to date
- NSDateFormatter * dateFormat = [[NSDateFormatter alloc] init]; // instantiate an NSDateFormatter object
- [DateFormat setDateFormat: @ "yyyy-MM-dd HH: mm: ss"]; // you can set the time format as needed.
- NSDate * date = [dateFormat dateFromString: @ "00:00:01"];
2. convert a date to a string
- NSDateFormatter * dateFormat = [[NSDateFormatter alloc] init]; // instantiate an NSDateFormatter object
- [DateFormat setDateFormat: @ "yyyy-MM-dd HH: mm: ss"]; // you can set the time format as needed.
-
- NSString * currentDateStr = [dateFormatter stringFromDate: [NSDate date];
Notes in Objective C -- string connection, @ colon in selector, time conversion, local variable
3. String Link
The Preprocessing Program automatically links adjacent string constants together. A string can be separated by 0 or multiple space characters. For example:
- "A" "character"
- "String"
-
- It is equivalent:
-
- "A character string"
A constant string object can be created by placing a @ character in front of the constant string. This type of object is NSConstantString. Similarly, the Preprocessing Program links adjacent constant string objects together: for example:
- @”a” @”character “
- @”string”
It is equivalent:
- @”a character string”
4. @ colon in the selector Method
When respondsToSelector: @ selector (method) is called, this method is only required when the method has a parameter: if this method does not need a parameter, you do not need to add this colon. Otherwise, compilation will not report an error, but the returned value is incorrect. Of course, if the method has multiple parameters, multiple colons are required. If the parameter has a name, the parameter name must be included.
For example, you can use the following methods:
- -(NSString*)toXmlString;
The call is similar:
- [self respondsToSelector:@selector(toXmlString)]
If the toXmlString method is defined:
- -(NSString*)toXmlString:(NSString*)prefix;
A colon must be added to the call, for example:
- [self respondsToSelector:@selector(toXmlString:)]
5. Soap time representation and time and date conversion
The date and time format of Soap during transmission is generally:
- Yyyy-MM-dd't'hh: mm: ss 'Z'
- (NSString *) dateToSoapString (NSDate *) date {
- NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
- [DateFormatter setDateFormat: @ "yyyy-MM-dd't'hh: mm: ss 'Z'"];
- NSString * dateString = [dateFormatter stringFromDate: date];
- [DateFormatter release];
- Return dateString;
- }
- Yyyy-MM-dd't'hh: mm: ss. SSS 'Z': 2010-07-08T07: 00: 53.000Z
For more information about time formats, see: http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
6. Local Variables
Local variables are generally defined in the method to save temporary data. The method parameter name is also a local variable. When a method is executed, any parameters passed by the method are copied to a local variable. Because the method uses a copy of the parameter, the original value passed through the method cannot be changed. Of course, if it is a class instance, you can change the value of an object or attribute in a class instance.
If a static keyword is added before the variable, the variable becomes a static variable. Static variables are initialized only once when the program starts execution, and only one value is saved from start to end:
For example:
- -(void) showPage{
- static int pageCount=0;
- …
- pageCount++;
- …
- }
PageCount of this method can record the number of showPage calls.
Summary:Objective-CMedium and manyStringI hope this article will help you!