Add documentation for operators and subscripting (#255)
This commit is contained in:
Родитель
da95151ad0
Коммит
25cd15c5d1
|
@ -125,3 +125,11 @@ In .NET a constructor must either succeed and return a (_hopefully_) valid insta
|
|||
In contrast, ObjC allows `init*` to return `nil` when an instance cannot be created. This is a common, but not general, pattern used in many of Apple's frameworks. In some other cases an `assert` can happen (and kill the current process).
|
||||
|
||||
The generator follow the same `return nil` pattern for generated `init*` methods. If a managed exception is thrown, then it will be printed (using `NSLog`) and `nil` will be returned to the caller.
|
||||
|
||||
## Operators
|
||||
|
||||
ObjC does not allow operators to be overloaded as C# does, so these are converted to class selectors.
|
||||
|
||||
["Friendly"](https://msdn.microsoft.com/en-us/library/ms229032(v=vs.110).aspx) named method are generated in preference to the operator overloads when found, and can produce an easier to consume API.
|
||||
|
||||
Classes that override the operators == and\or != should override the standard Equals (Object) method as well.
|
||||
|
|
75
docs/ObjC.md
75
docs/ObjC.md
|
@ -64,6 +64,34 @@ would create an ObjC category like this one:
|
|||
|
||||
When a single managed type extends several types then multiple ObjC categories are generated.
|
||||
|
||||
### Subscripting
|
||||
|
||||
Managed indexed properties are converted into object subscripting. For example:
|
||||
|
||||
```
|
||||
public bool this[int index] {
|
||||
get { return c[index]; }
|
||||
set { c[index] = value; }
|
||||
}
|
||||
```
|
||||
|
||||
would create ObjC similar to :
|
||||
|
||||
```
|
||||
- (id)objectAtIndexedSubscript:(int)idx;
|
||||
- (void)setObject:(id)obj atIndexedSubscript:(int)idx;
|
||||
```
|
||||
|
||||
which can be used via the ObjC subscripting syntax:
|
||||
|
||||
```
|
||||
if ([intCollection [0] isEqual:@42])
|
||||
intCollection[0] = @13;
|
||||
```
|
||||
|
||||
Depending on the type of your indexer, indexed or keyed subscripting will be generated where appropriate.
|
||||
|
||||
This [article](http://nshipster.com/object-subscripting/) is a great introduction to subscripting.
|
||||
|
||||
## Main differences with .NET
|
||||
|
||||
|
@ -106,4 +134,49 @@ Objective-C surfaced API:
|
|||
@end
|
||||
```
|
||||
|
||||
Here we can see that `initWithId:` has been marked as unavailable.
|
||||
Here we can see that `initWithId:` has been marked as unavailable.
|
||||
|
||||
### Operator
|
||||
|
||||
ObjC does not support operator overloading as C# does, so operators are converted to class selectors:
|
||||
|
||||
```
|
||||
public static AllOperators operator + (AllOperators c1, AllOperators c2)
|
||||
{
|
||||
return new AllOperators (c1.Value + c2.Value);
|
||||
}
|
||||
```
|
||||
|
||||
to
|
||||
|
||||
```
|
||||
+ (instancetype)addition:(Overloads_AllOperators *)anObjectC1 c2:(Overloads_AllOperators *)anObjectC2;
|
||||
```
|
||||
|
||||
However, some .NET languages do not support operator overloading, so it is common to also include a ["friendly"](https://msdn.microsoft.com/en-us/library/ms229032(v=vs.110).aspx) named method in addition to the operator overload.
|
||||
|
||||
If both the operator version and the "friendly" version are found, only the friendly version will be generated.
|
||||
|
||||
```
|
||||
public static AllOperatorsWithFriendly operator + (AllOperatorsWithFriendly c1, AllOperatorsWithFriendly c2)
|
||||
{
|
||||
return new AllOperatorsWithFriendly (c1.Value + c2.Value);
|
||||
}
|
||||
|
||||
public static AllOperatorsWithFriendly Add (AllOperatorsWithFriendly c1, AllOperatorsWithFriendly c2)
|
||||
{
|
||||
return new AllOperatorsWithFriendly (c1.Value + c2.Value);
|
||||
}
|
||||
```
|
||||
|
||||
becomes:
|
||||
|
||||
```
|
||||
+ (instancetype)addC1:(Overloads_AllOperatorsWithFriendly *)anObjectC1 c2:(Overloads_AllOperatorsWithFriendly *)anObjectC2;
|
||||
```
|
||||
|
||||
### Equality operator
|
||||
|
||||
In general operator == in C# is handled as a general operator as noted above.
|
||||
|
||||
However, if the "friendly" Equals operator is found, both operator == and operator != will be skipped in generation.
|
||||
|
|
Загрузка…
Ссылка в новой задаче