Objective-C 程序设计语言采用特定的语法,来定义类和方法、调用对象的方法、动态地扩展类,以及创建编程接口,来解决具体问题。Objective-C 作为 C 程序设计语言的超集,支持与 C 相同的基本语法。有C#语言的编程经验,那么Objective-C将更容易上手,本文将从各个方面对比C#与Objective-C的语法。

字符串

声明:

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 = 10;
string age = i.ToString();
int intAge = int.Parse(age); Objective-C:
NSInteger i = 10;
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) == 0; // 忽略大小写
bool isEqual = string.Compare(strA, strB, StringComparison.InvariantCultureIgnoreCase) == 0;
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 == 0);
BOOL empty = (value == nil || value.length == 0 || [[value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0); // 同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(20);
builder.Append("lwme");
builder.AppendFormat(".{0}.{1}", "cnblogs", "com"); Objective-C:
NSMutableString *builder = [NSMutableString stringWithCapacity:20];
[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[0]
data[0]="two"; Objective-C:
[data objectAtIndex:0]
data[0] // objc 2.0 新语法
data[0]=@"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>(20);
List<string> list = new List<string> { "lwme", "cnblogs" };
list.Add("lwme");
list.AddRange(new[] { "l", "w", "m", "e" });
list.Insert(0, "lwme");
list.Remove("lwme");
list.RemoveAt(0);
list.Clear();
bool isContain = list.Contains("lwme");
bool isContain = list.Contains("lwme", StringComparer.InvariantCultureIgnoreCase); Objective-C:
NSMutableArray *list = [NSMutableArray array];
NSMutableArray *list = [NSMutableArray arrayWithCapcity: 20];
NSMutableArray *list = [@[@"lwme", @"cnblogs"] mutableCopy];
[list addObject:@"lwme"];
[list addObjectsFromArray:@[@"lwme, @"cnblogs"]];
[list insertObject:@"lwme", atIndex:0];
[list removeObject:@"lwme"];
[list removeObjectAtIndex:0];
[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>(20);
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: 20];
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 = 1;
long li = 1;
float fi = 1.1;
double di = 1.1;
bool b = true; Objective-C:
int ii = 1;
long li = 1;
float fi = 1.1;
double di = 1.1;
bool b = true;
BOOL b = YES;
NSNumber ii = [NSNumber numberWithInt: 1];
NSNumber li = [NSNumber numberWithLong: 1];
NSNumber fi = [NSNumber numberWithFloat: 1.1];
NSNumber di = [NSNumber numberWithDouble: 1.1];
NSNumber b = [NSNumber numberWithBOOL: YES];
NSNumber ii = @1;
NSNumber li = @1;
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(11);
Person p = Person.CreatePerson(11);
p.setFullName("lwme", "cnblogs");
p.FirstName = "lwme";
p.PrintAge();
string fullName = p.GetFullName();
int age = p.Age; Objective-C:
Person *p = [[Person alloc] initWithAge:11];
Person *p = [Person CreateWithAge:11];
[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(11);
string val = p.ToString(); Objective-C:
ChildPerson *p = [[ChildPerson alloc] initWithAge:11];
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 = 0,
Horizontal = 1
} Objective-C
typedef enum Orientation : NSUInteger {
OrientationVertical = 0,
OrientationHorizontal = 1
} 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 = 0;
objc_property_t * properties = class_copyPropertyList([Person class], &propertyCount);
for (unsigned int i = 0; 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#到Objective-C的更多相关文章

  1. Automake

    Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...

  2. Objective C中的ARC的修饰符的使用---- 学习笔记九

    #import <Foundation/Foundation.h> @interface Test : NSObject /** * 默认的就是__strong,这里只是做示范,实际使用时 ...

  3. Objective的字符串拼接 似乎没有Swift方便,但也可以制做一些较为方便的写法

    NSString *str1 = @"字符串1"; NSString *str2 = @"字符串2"; //在同样条件下,Objective的字符串拼接 往往只 ...

  4. [转] 从 C 到 Objective C 入门1

    转自: http://blog.liuhongwei.cn/iphone/objective-c/ 进军iPhone开发,最大的难点之一就是怪异的Objective C语法了.不过,了解之后才发现,原 ...

  5. Objective C运行时(runtime)

    #import <objc/runtime.h> void setBeingRemoved(id __self, SEL _cmd) { NSLog(@"------------ ...

  6. Objective C ARC 使用及原理

    手把手教你ARC ,里面介绍了ARC的一些特性, 还有将非ARC工程转换成ARC工程的方法 ARC 苹果官方文档 下面用我自己的话介绍一下ARC,并将看文档过程中的疑问和答案写下来.下面有些是翻译,但 ...

  7. Objective -C学习笔记之字典

    //字典:(关键字 值) // NSArray *array = [NSArray array];//空数组 // NSDictionary *dictionary = [NSDictionary d ...

  8. 刨根问底Objective-C Runtime

    http://chun.tips/blog/2014/11/05/bao-gen-wen-di-objective%5Bnil%5Dc-runtime-(2)%5Bnil%5D-object-and- ...

  9. Objective-C( Foundation框架 一 字符串)

    Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重 ...

  10. Objective C类方法load和initialize的区别

    Objective C类方法load和initialize的区别   过去两个星期里,为了完成一个工作,接触到了NSObject中非常特别的两个类方法(Class Method).它们的特别之处,在于 ...

随机推荐

  1. js动态生成input指定My97DatePicker时间问题

    js生成的input指定onclick时间: 以下1.2为错误: onclick="WdatePicker()"; onclick=WdatePicker(); 若指定到windo ...

  2. javascript 原型详解

    引:http://www.cnblogs.com/wangfupeng1988/p/3978131.html 1.什么是javascript原型 每一个函数都有prototype属性(默认生成的)和原 ...

  3. JSTL和EL的区别

    JSTL(JSP Standard Tag Library,JSP标准标签库)是一个不断完善的开放源代码的JSP标签库,是由apache的jakarta小组来维护的.JSTL只能运行在支持JSP1.2 ...

  4. <a>标签的四种状态

    a:link  a:visited  a:hover   a:active

  5. Lua metatable & metamethod

    [Lua metatable & metamethod] Lua中的每个值都有一套预定义的操作集合,比如数字是可以相加的,字符串是可以连接的,但是对于两个table类型,则不能直接进行“+”操 ...

  6. 【Python全栈笔记】00 12-14 Oct Linux 和 Python 基础

    Linux 基础认识 更加稳定,安全,开源 设置好ssh协议后可以通过windows系统连接Linux,基于ssh协议进行通信 '/' 为根目录 cd / -> 切换到根目录 ls -lh 列出 ...

  7. java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView

    最近在学习drawerLayout时,遇到这个bug.如下示: java.lang.ClassCastException: android.widget.RelativeLayout cannot b ...

  8. Block的copy和循环引用的问题

    在实际开发中,发现使用Block有着比delegate和notification更简洁的优势.于是在目前的项目中大量的使用block. 在我的头文件我是这样声明使用block的. 我将block声明为 ...

  9. (转)C#中两个问号和一个问号 ??

    小问题难倒很多人.今天发现了这个问题,搜了很长时间才看到记录下. 实例:dt.Columns.Add(firstRow.GetCell(i).StringCellValue ?? string.For ...

  10. DL4J (DeepLearning for java)

    http://deeplearning4j.org/lstm.html A Beginner’s Guide to Recurrent Networks and LSTMs Contents Feed ...