字符串

声明:

C#:
string name = “lwme.cnblogs.com”; Objective-C:
NSString *name = @”lwme.cnblogs.com”;

字符串前面的@符号是objc在标准C语言基础上添加的特性,"SteveZ"前面有一个@符号表示字符串应该作为Cocoa的NSString来处理,是把C string转换成NSString的一个简写。

获取长度:

C#:
name.Length
 
Objective-C:
[name length]
name.length

转换:

C#:
int i = ;
string age = i.ToString();
int intAge = int.Parse(age);
 
Objective-C:
NSInteger i = ;
NSString *age = [@(i) stringValue]; // 转换成NSNumber再获取字符串值
NSString *age = [NSString stringWithFormat:@"%d", i];
NSInteger intAge = [age integerValue];

拼接:

C#:
string host = "lwme" + ".cnblogs.com";
 
Objective-C:
NSString *host = @"lwme" @".cnblogs.com"
NSString *host = [@"lwme" stringByAppendingString:@".cnblogs.com"];
NSString *host = [NSString stringWithFormat:@"%@%@", @"lwme", @".cnblogs.com"];

比较:

C#:
bool isEqual = strA == strB;
bool isEqual = string.Compare(strA, strB,  true) == ; // 忽略大小写
bool isEqual = string.Compare(strA, strB,  StringComparison.InvariantCultureIgnoreCase) == ;
bool isStartWith = strA.StartsWith(strB);
bool isStartWith = strA.StartsWith(strB, StringComparison.InvariantCultureIgnoreCase);
bool isEndWith = strA.EndsWith(strB);
bool isEndWith = strA.EndsWith(strB, StringComparison.InvariantCultureIgnoreCase);
bool isContain = strA.Contains(strB);
bool isContain = strA.Contains(strB, StringComparison.InvariantCultureIgnoreCase);
 
Objective-C:
BOOL isEqual = [strA isEqualToString: strB];
BOOL isEqual = [strA caseInsensitiveCompare:strB] == NSOrderedSame; // 忽略大小写
BOOL isEqual = [strA compare:strB options:NSCaseInsensitiveSearch] == NSOrderedSame;
BOOL isStartWith = [strA hasPrefix:strB];
BOOL isEndWith = [strA hasSuffix:strB];
BOOL isContain = [strA rangeOfString:strB].location != NSNotFound;
BOOL isContain = [strA rangeOfString:strB options:NSCaseInsensitiveSearch].location != NSNotFound;

检查是否为空:

C#:
bool empty = string.IsNullOrEmpty(value);
bool empty = string.IsNullOrWhiteSpace(value);
 
Objective-C:
BOOL empty = (value == nil || value.length == );
BOOL empty = (value == nil || value.length == || [[value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == );  //  同IsNullOrWhiteSpace

转换大小写:

C#:
string name = value.ToLower();
string name = value.ToUpper();
 
Objective-C:
NSString *name = [value lowercasestring];
NSString *name = [value uppercasestring];

格式化:

C#:
float x = 2.43534f;
string s = x.ToString(“n2″);
string s = string.Format("{0} {1}", "lwme", "cnblogs");
 
Objective-C:
float x = 2.34454f;
NSString *s = [NSString stringWithFormat: @"%.2f", x];
NSString *s = [NSString stringWithFormat: @"%@ %@", @"lwme", @"cnblogs"];

可变字符串

objc和C#一样,字符串都是不可变的,要改变字符串则分别需要使用StringBuilder、NSMutableString

C#:
StringBuilder builder = new StringBuilder();
builder.Append("lwme");
builder.AppendFormat(".{0}.{1}", "cnblogs", "com");
 
Objective-C:
NSMutableString *builder = [NSMutableString stringWithCapacity:];
[builder appendString:@"lwme"];
[builder appendFormat:@".%@.%@", @"cnblogs", @"com"];

数组

声明:

C#:
string[] data = {};
string[] data = {"one", "two"};
 
Objective-C:
// 旧语法
NSArray* data = [NSArray arrayWithObjects:@"one", @"two", nil]; // (定义数组时用逗号分隔对象列表,并且必须以nil结尾)
// objc 2.0新语法
NSArray *data = @[]; //空数组
NSArray* data = @[@"one", @"two"];

获取长度:

C#:
data.Length
 
Objective-C:
[data count]
data.count

获取/设置元素值:

C#:
data[]
data[]="two";
 
Objective-C:
[data objectAtIndex:]
data[] // objc 2.0 新语法
data[]=@"two";

查找元素:

C#:
int index = data.IndexOf("one");
 
Objective-C:
int index = [data indexOfObject:@"one"];

遍历:

C#:
foreach (string item in array) ...
 
Objective-C:
for (NSString *item in array) ...

可变数组

objc和C#一样,数组是不可变的,如果需要改变数组的元素,则分别要用到List<T>、NSMutableArray。

C#:
List<string> list = new List<string>();
List<string> list = new List<string>();
List<string> list = new List<string> { "lwme", "cnblogs" };
list.Add("lwme");
list.AddRange(new[] { "l", "w", "m", "e" });
list.Insert(, "lwme");
list.Remove("lwme");
list.RemoveAt();
list.Clear();
bool isContain = list.Contains("lwme");
bool isContain = list.Contains("lwme", StringComparer.InvariantCultureIgnoreCase);
 
Objective-C:
NSMutableArray *list = [NSMutableArray array];
NSMutableArray *list = [NSMutableArray arrayWithCapcity: ];
NSMutableArray *list = [@[@"lwme", @"cnblogs"] mutableCopy];
[list addObject:@"lwme"];
[list addObjectsFromArray:@[@"lwme, @"cnblogs"]];
[list insertObject:@"lwme", atIndex:];
[list removeObject:@"lwme"];
[list removeObjectAtIndex:];
[list removeAllObjects];
BOOL isContain = [list containsObject:@"lwme"];

字典

在objc中有不可变的字典NSDictionary,在C#中没有对应的,而可变的字典NSMutableDictionary对应C#中的Dictionary<TKey,TValue>。

C#:
Dictionary<string, string> dict = new Dictionary<string, string>();
Dictionary<string, string> dict = new Dictionary<string, string>();
Dictionary<string, string> dict = new Dictionary<string, string> { {"name", "lwme"}, {"host", "cnblogs"} };
dict.Add("name", "lwme");
dict["name"] = "lwme";
dict.Remove("name");
dict.Clear();
dict.ContainsKey("name");
string name = dict["name"]; // dict.TryGetValue("name", out ...)
 
Objective-C:
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: @"lwme", @"name", @"cnblogs", @"host", nil]; // 旧语法
NSDictionary *dict = @{@"name": @"lwme", @"host": @"cnblogs"}; // objc2.0语法
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity: ];
NSMutableDictionary *dict = [@{@"name": @"lwme", @"host": @"cnblogs"} mutableCopy];
[dict setObject:@"lwme" forKey:@"name"];
[dict removeObjectForKey:@"name"];
[dict removeAllObjects];
BOOL isContain = [[dict allKeys] containsObject:@"name"];
BOOL isContain = [dict objectForKey:@"name"] != nil;
NSString *name = [dict objectForKey:@"name"];
NSString *name = dict[@"name"];

各种数值

objc中有c语言的基本类型如int、float、struct等,还有它们的封装类型NSNumber,由于NSArray、NSDictionary只能存储对象,不能存储任何基本类型,所以这些基本类型需要用NSNumber来包装。

C#:
int ii = ;
long li = ;
float fi = 1.1;
double di = 1.1;
bool b = true;
 
Objective-C:
int ii = ;
long li = ;
float fi = 1.1;
double di = 1.1;
bool b = true;
BOOL b = YES;
NSNumber ii = [NSNumber numberWithInt: ];
NSNumber li = [NSNumber numberWithLong: ];
NSNumber fi = [NSNumber numberWithFloat: 1.1];
NSNumber di = [NSNumber numberWithDouble: 1.1];
NSNumber b = [NSNumber numberWithBOOL: YES];
NSNumber ii = @;
NSNumber li = @;
NSNumber fi = @1.1;
NSNumber di = @1.1;
NSNumber b = @YES;

类、属性、方法

objc的类分成.h头文件和.m实现文件,.h头文件里定义所有公开的方法、属性、字段,.m实现文件里实现公开的方法以及私有的字段、方法。

C#:
public class Person
{
  private int privateField;
  public int Age {get;set;}
  public string FirstName {get;set;}
  public string LastName {get;set;}
   
  public Person(int age) {
    this.Age = age;
    this.OnAgeChange(age);
  }
   
  private void OnAgeChange(int age) {
   
  }
   
  public string GetFullName() {
    return this.FirstName + " " + this.LastName;
  }
   
  public void SetFullName(string firstName, string lastName) {
    this.FirstName = firstName;
    this.LastName = lastName;
  }
   
  public virtual void PrintAge(){
    Console.WriteLine("{0}", this.Age);
  }
   
  public static Person CreatePerson(int age) {
    return new Person(age);
  }
}
 
Objective-C:
Person.h:
@interface Person : NSObject
-(id)initWithAge:(int)age; // 构造器
@property(nonatomic) int age; // 属性
@property(nonatomic) NSString *firstName;
@property(nonatomic) NSString *lastName;
-(NSString*)getFullName; // 方法
-(void)setFullName:(NSString *)firstName andLastName:(NSString *)lastName;
-(void)printAge;
+(id)createPersonWithAge:(int)age;
@end
 
Person.m:
@interface Person()
{
   int _privateField; // 私有字段
}
@end
 
@implementation Person
 
@synthesize age, firstName, lastName;
 
-(id)initWithAge:(int)age
{
   if(self = [super init])
   {
      self.age = age;
      [self onAgeChange:age];
   }
   return self;
}
-(void)onAgeChange:(int)age
{
 
}
-(NSString*)getFullName
{
   return [NSString stringWithFormat:"%@ %@", self.firstName, self.lastName];
}
-(void)setFullName:(NSString *)firstName andLastName:(NSString *)lastName
{
   self.firstName = firstName;
   self.lastName = lastName;
}
-(void)printAge
{
   NSLog(@"%i", self.age);
}
+(id)createPersonWithAge:(int)age
{
   return [[Person alloc] initWithAge:age];
}
@end

访问对象属性:

C#:
Person p = new Person();
Person p = Person.CreatePerson();
p.setFullName("lwme", "cnblogs");
p.FirstName = "lwme";
p.PrintAge();
string fullName = p.GetFullName();
int age = p.Age;
 
Objective-C:
Person *p = [[Person alloc] initWithAge:];
Person *p = [Person CreateWithAge:];
[p setFullName:@"lwme" andLastName:@"cnblogs"];
p.firstName = @"lwme";
[p PrintAge];
NSString *fullName = [p getFullName];
int age = p.Age;

继承及调用父类方法:

C#:
public class ChildPerson: Person
{
  public ChildPerson(int age): base(age){}
   
  public override void PrintAge() {
    base.PrintAge();
    Console.WriteLine("print age in childperson");
  }
}
 
Objective-C:
ChildPerson.h
@interface ChildPerson:Person
@end
 
ChildPerson.m
@interface ChildPerson()
@end
 
@implementation ChildPerson
-(void)printAge
{
  [super printAge];
  NSLog(@"print age in childperson");
}
@end

获取描述信息:

C#:
ChildPerson p = new ChildPerson();
string val = p.ToString();
 
Objective-C:
ChildPerson *p = [[ChildPerson alloc] initWithAge:];
NSString *val = [p description];

接口及事件

objc中的协议可以对应C#中的接口、事件,其中事件一般声明为delegate名称的属性,在使用的时候只需要设置delete就可以绑定所有事件。

声明及实现:

C#
public interface IEquatable<T>
{
  bool Equals(T obj);
}
 
public class Person : IEquatable<MyClass>
{
  public Action<Person> onAgeChanged;
 
  public int Age { get; set; }
   
  public bool Equals(Person p)
  {
    return p.Age == this.Age;
  }
   
  public void SetAge(int age) {
    this.Age = age;
    if (onAgeChanged != null)
      onAgeChanged(this);
  }
}
 
Objective-C
@protocol IEquatable
@required
  -(bool)equals: (Person*)obj;
@end
 
@protocol IChangeAction
@optional
  -(void)onAgeChange: (Person*)obj;
@end
 
@interface Person : NSObject<IEquatable>
@property(nonatomic) int age;
@property(nonatomic, weak)id<IChangeAction> delegate;
-(void)setAge:(int)age;
@end
 
@implementation Person
@synthesize age;
 
-(bool)equals:(Person*)p
{
   return p.age == self.age;
}
-(void)setAge:(int)age
{
   self.age = age;
   if ([self.delegate respondsToSelector:@selector(onAgeChange:)])
    [self.delegate onAgeChange:self];
}
@end

枚举

声明:

C#
public enum Orientation: uint {
  Vertical = ,
  Horizontal =
}
 
Objective-C
typedef enum Orientation : NSUInteger {
  OrientationVertical = ,
  OrientationHorizontal =
} Orientation;

使用:

C#
public Orientation TextAlignment {get;set} // 属性
 
label.TextAlignment = Orientation.Horizontal;
 
Objective-C
@property(nonatomic, assign) Orientation textAlignment;
 
label.textAlignment = OrientationHorizontal;

反射

获取对象类型:

C#:
Type t = typeof(Person);
 
Objective-C:
Class c = [Person class];

获取实例类型:

C#:
Person p = new Person();
Type t = p.GetType();
 
Objective-C:
Person *p = [[Person alloc] init];
Class c = p.class;

获取类型名称:

C#:
string name = type.Name;
string name = typeof(Person).Name;
 
Objective-C:
NSString *name = NSStringFromClass(p.class);
NSString *name = NSStringFromClass([Person class]);

判断实例类型:

C#:
bool isPerson = p is Person;
 
Objective-C:
BOOL isPerson = [p isKindOfClass:c];

判断是否继承:

C#:
bool isInherited = p is Person;
bool isInherited = p.GetType().IsSubclassOf(typeof(Person));
bool isInherited = typeof(ChildPerson).IsSubclassOf(typeof(Person));
bool isInherited = typeof(Person).IsAssignableFrom(typeof(ChildPerson));
 
Objective-C:
BOOL isInherited = [[p class] isSubclassOfClass:[Person class]];
BOOL isInherited = [[ChildPerson class] isSubclassOfClass:[Person class]];

判断是否实现接口/协议:

C#:
bool isImplemented = p is IEquatable;
bool isImplemented = typeof(IEquatable<>).IsAssignableFrom(p.GetType());
bool isImplemented = typeof(IEquatable<>).IsAssignableFrom(typeof(Person));
bool isImplemented = typeof(Person).GetInterfaces().Contains(typeof(IEquatable<>));
 
Objective-C:
BOOL isImplemented = [p class conformsToProtocol:@protocol(IEquaatable)];

从类型名称创建实例:

C#:
object instance = Activator.CreateInstance(Type.GetType("ChildPerson"));
 
Objective-C:
id instance = [[NSClassFromString("ChildPerson") alloc] init];

获取、调用方法:

C#:
MethodInfo method = type.GetMethod("PrintAge");
bool hasMethod = method != null;
method.Invoke(p, null);
 
Objective-C:
SEL method = @selector(printAge);
BOOL hasMethod = [p respondsToSelector: method];
[p performSelector:method];

获取属性:

C#:
object age = p.GetType().GetProperty(“Age”).GetValue(p, null);
foreach(PropertyInfo pi in typeof(Person).GetProperties())
{
  // pi.Name
}
 
Objective-C:
id age = [p valueForKey:@"Age"];
unsigned int propertyCount = ;
objc_property_t * properties = class_copyPropertyList([Person class], &propertyCount);
for (unsigned int i = ; i < propertyCount; ++i) {
  objc_property_t property = properties[i];
  const char * name = property_getName(property);
}
free(properties);

扩展方法

objc里的Category可以很好的实现C#中的扩展方法。

C#
public static class PersonExtension {
  public static void PrintFullName(this Person p) {
    Console.WriteLine(p.GetFullName());
  }
}
 
Objective-C
Person+PrintExtension.h
#import "Person.h"
@interface Person (PrintExtension)
-(void)PrintFullName;
@end
 
Person+PrintExtension.m
@implementation Person (PrintExtension)
-(void)PrintFullName
{
  NSLog(@"%@", [self getFullName]);
}
@end

异常处理

C#
  try {
   
  } catch (Exception ex) {
   
  } finally {
   
  }
 
Objective-C
  @try {
 
  }
  @catch (NSException *exception) {
 
  }
  @finally {
 
  }

命名空间

objc没有命名空间机制,直接从包或者当前项目引入头文件即可。

引用:

C#
using System;
using System.Text;
using MyCompany.CustomNamespace;
 
Objective-C
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "CustomClass.h"

内存管理

objc中有ARC机制对应.net 中的GC,详细的可以看:http://snakcakeblog.tumblr.com/post/47765862590/grudge-match-c-gc-vs-objective-c-arc

程序集

在C#当中,源代码一般被编译成程序集,而在objc中,都是编译成静态链接库,而且引用方式也不相同。

参考

http://www.infragistics.com/community/blogs/stevez/archive/2013/05/09/c-to-objective-c-the-ultimate-guide.aspx(本文内容主要来自它,并做了一些更改)

http://developer.apple.com/library/ios/#referencelibrary/GettingStarted/RoadMapiOSCh/chapters/Introduction.html

http://www.anotherchris.net/csharp/objective-c-by-example-for-a-csharp-developer/

http://www.cnblogs.com/chijianqiang/archive/2012/06/22/objc-category-protocol.html

http://www.techotopia.com/index.php/Working_with_String_Objects_in_Objective-C

http://overooped.com/post/41803252527/methods-of-concurrency

从C# 到 OC的更多相关文章

  1. iOS代码规范(OC和Swift)

    下面说下iOS的代码规范问题,如果大家觉得还不错,可以直接用到项目中,有不同意见 可以在下面讨论下. 相信很多人工作中最烦的就是代码不规范,命名不规范,曾经见过一个VC里有3个按钮被命名为button ...

  2. 用C语言封装OC对象(耐心阅读,非常重要)

    用C语言封装OC对象(耐心阅读,非常重要) 本文的主要内容来自这里 前言 做iOS开发的朋友,对OC肯定非常了解,那么大家有没有想过OC中NSInteger,NSObject,NSString这些对象 ...

  3. 嵌入式&iOS:回调函数(C)与block(OC)传 参/函数 对比

    C的回调函数: callBack.h 1).声明一个doSomeThingCount函数,参数为一个(无返回值,1个int参数的)函数. void DSTCount(void(*CallBack)(i ...

  4. 嵌入式&iOS:回调函数(C)与block(OC)回调对比

    学了OC的block,再写C的回调函数有点别扭,对比下区别,回忆记录下. C的回调函数: callBack.h 1).定义一个回调函数的参数数量.类型. typedef void (*CallBack ...

  5. WebViewJavascriptBridge源码探究--看OC和JS交互过程

    今天把实现OC代码和JS代码交互的第三方库WebViewJavascriptBridge源码看了下,oc调用js方法我们是知道的,系统提供了stringByEvaluatingJavaScriptFr ...

  6. OC泛型

    OC泛型 泛型是程序设计语言的一种特性,他主要是为了限制类型的,比如OC中的数组,你可以限制他里面装的是NSString类型,泛型的话JAVA和C++都有的,大家要是对泛型不了解的话可以去百度一下. ...

  7. iOS学习15之OC集合

    1.数组类 1> 回顾C语言数组 数组是一个有序的集合, 来存储相同数据类型的元素. 通过下标访问数组中的元素,下标从 0 开始. 2> 数组 数组是一个有序的集合,OC中的数组只能存储对 ...

  8. JS 与OC 交互篇

    完美记录交互 CSDN博客: (OC调用JS) http://blog.csdn.net/lwjok2007/article/details/47058101 (JS调用OC) http://blog ...

  9. OC中加载html5调用html方法和修改HTML5内容

    1.利用webView控件加载本地html5或者网络上html5 2.设置控制器为webView的代理,遵守协议 3.实现代理方法webViewDidFinishLoad: 4.在代理方法中进行操作H ...

  10. iOS开发--JS调用原生OC篇

    JS调用原生OC篇 方式一(反正我不用) 第一种方式是用JS发起一个假的URL请求,然后利用UIWebView的代理方法拦截这次请求,然后再做相应的处理. 我写了一个简单的HTML网页和一个btn点击 ...

随机推荐

  1. DOM-based xss

    这个漏洞往往存在于客户端脚本,如果一个Javascript脚本访问需要参数的URL,且需要将该信息用于写入自己的页面,且信息未被编码,那么就有可能存在这个漏洞. (一)DOM—based XSS漏洞的 ...

  2. 应用PHPCMS V9轻松完成WAP手机网站搭建全教程

    用PHPCMS最新发布的V9搭建了PHPCMS研究中心网站(http://phpcms.org.cn)完成后,有用户提出手机访问的问题,于是着手搭建WAP无线站(wap.phpcms.org.cn). ...

  3. ORA-00931: missing identifier ORA-06512: at "SYS.DBMS_UTILITY"

    Database db = DatabaseFactory.CreateDatabase();            string sql = "SELECT * FROM table&qu ...

  4. jquery中$.ajax

    $.ajax({ type : 'post', url : '/edm/testEmail.php', data: {tId:tId, sId:sId ,testEmail:testEmail}, d ...

  5. 类型安全且自动管理内存的返回 std::string 的 sprintf 实现

    在这篇博文里,我提到了一个例子,说的是使用C++实现类型安全的printf.这个例子很惊艳,但是在我写程序的时候,并非那么"迫切"地需要它出现在我的工具箱中,因为它并不比普通的pr ...

  6. java笔记--使用事件分配线程更新Swing控件

    使用事件分配线程更新Swing控件: Swing并不是线程安全的,如果在多个线程中更新Swing控件,则很可能造成程序崩溃. 为了避免这种问题,可以使用时间分配线程来更新Swing控件. EventQ ...

  7. JVM<一>----------运行时数据区域

    参考:1.JVM Specification: http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.5 2.< ...

  8. 解决ie文本框不能输入和获取焦点问题

    解决办法:    从正常的机器上拷贝c:\windows\system32\mshtmled.dll到本机的system32目录下即可.或者从安装盘中提取该文件.    加载mshtmled.dll: ...

  9. magic-encoding

    (文章都是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) 今天页面跳转都出问题了,各种方法都试过了, log里说语法错误,问了pp,他说是汉字的原因...果 ...

  10. Java 23种设计模式

    转自: http://zz563143188.iteye.com/blog/1847029 ; i<count; i++){ list.add(new MailSender()); } } pu ...