Explain how to use multiple strings in Objective-C

Source: Internet
Author: User

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

 
 
  1. NSDateFormatter * dateFormat = [[NSDateFormatter alloc] init]; // instantiate an NSDateFormatter object
  2. [DateFormat setDateFormat: @ "yyyy-MM-dd HH: mm: ss"]; // you can set the time format as needed.
  3. NSDate * date = [dateFormat dateFromString: @ "00:00:01"];

2. convert a date to a string

 
 
  1. NSDateFormatter * dateFormat = [[NSDateFormatter alloc] init]; // instantiate an NSDateFormatter object
  2. [DateFormat setDateFormat: @ "yyyy-MM-dd HH: mm: ss"]; // you can set the time format as needed.
  3.  
  4. 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:

 
 
  1. "A" "character"
  2. "String"
  3.  
  4. It is equivalent:
  5.  
  6. "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:

 
 
  1. @”a” @”character “  
  2. @”string” 

It is equivalent:

 
 
  1. @”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:

 
 
  1. -(NSString*)toXmlString; 

The call is similar:

 
 
  1. [self respondsToSelector:@selector(toXmlString)] 

If the toXmlString method is defined:

 
 
  1. -(NSString*)toXmlString:(NSString*)prefix; 

A colon must be added to the call, for example:

 
 
  1. [self respondsToSelector:@selector(toXmlString:)] 

5. Soap time representation and time and date conversion

The date and time format of Soap during transmission is generally:

 
 
  1. Yyyy-MM-dd't'hh: mm: ss 'Z'
  2. (NSString *) dateToSoapString (NSDate *) date {
  3. NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
  4. [DateFormatter setDateFormat: @ "yyyy-MM-dd't'hh: mm: ss 'Z'"];
  5. NSString * dateString = [dateFormatter stringFromDate: date];
  6. [DateFormatter release];
  7. Return dateString;
  8. }
  9. 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:

 
 
  1. -(void) showPage{  
  2. static int pageCount=0;  
  3. …  
  4. pageCount++;  
  5. …  

PageCount of this method can record the number of showPage calls.

Summary:Objective-CMedium and manyStringI hope this article will help you!

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.