В своём докладе Влад будет говорить об использовании STL, управлении памятью, как работает ARC и C++, лямбды и блоки. И самое главное: где и как можно «выстрелить себе в ногу».
2. Why Objective-C++
1. Compile time
2. Efficiency
3. Aggregate initialization
4. Type safety
5. Powerful standard library
6. A lot of 3rdparty libraries1
1. https://github.com/fffaraz/awesome-cpp
6. std::vector<T>
std::vector<CGPoint> v = …;
// C++98
for (std::vector<CGPoint>::const_iterator i = v.begin(); i != v.end(); ++i)
{
// do something with p
}
// C++11
for (auto const & p : v)
{
// do something with p
}
7. std::vector<T>
// Warning: Incompatible pointer types sending 'NSNumber *' to parameter of type
'NSString * _Nonnull'
NSMutableArray<NSString *> * a = [@[@""] mutableCopy];
[a addObject:@0];
8. std::vector<T>
// Warning: Incompatible pointer types sending 'NSNumber *' to parameter of type
'NSString * _Nonnull'
NSMutableArray<NSString *> * a = [@[@""] mutableCopy];
[a addObject:@0];
// Compile time error
std::vector<NSString *> v = {@""};
v.push_back(@0);
9. std::vector<T>
// Warning: Incompatible pointer types sending 'NSNumber *' to parameter of type
'NSString * _Nonnull'
NSMutableArray<NSString *> * a = [@[@""] mutableCopy];
[a addObject:@0];
// Compile time error
std::vector<NSString *> v = {@""};
v.push_back(@0);
// In .mm file: compile time error!
// Cannot initialize a parameter of type 'NSString * _Nonnull' with an rvalue of
type 'NSNumber *'
NSMutableArray<NSString *> * a = [@[@""] mutableCopy];
[a addObject:@0];
21. smart pointers
vector<shared_ptr<Dog>> v {make_shared<Dog>(droopy),
make_shared<Dog>(goofy),
make_shared<Dog>(pluto)};
for (const auto & d : v)
{
d->bark();
}
auto p1 = make_shared<Dog>(droopy);
auto p2 = p1;
23. smart pointers
vector<unique_ptr<Dog>> v {make_unique<Dog>(droopy),
make_unique<Dog>(goofy),
make_unique<Dog>(pluto)};
for (const auto & d : v)
{
d->bark();
}
auto p1 = make_unique<Dog>(droopy);
auto p2 = p1; // Compile time error!
24. smart pointers
vector<unique_ptr<Dog>> v {make_unique<Dog>(droopy),
make_unique<Dog>(goofy),
make_unique<Dog>(pluto)};
for (const auto & d : v)
{
d->bark();
}
auto p1 = make_unique<Dog>(droopy);
auto p2 = move(p1); // Ok
30. auto
auto x; // Compile time error!
- (CGFloat)getY
{
// determine y
return y;
}
float y = self.getY;
31. auto
auto x = 0; // Compile time error!
- (CGFloat)getY
{
// determine y
return y;
}
auto y = self.getY; // y is CGFloat
32. auto
^ UIImage * (NSData * result, NSUInteger resultCode, NSDictionary * info)
{
// do something with data
// return image
return image;
};
33. auto
UIImage * (^b)(NSData * result, NSUInteger resultCode, NSDictionary * info) = ^
UIImage * (NSData * result, NSUInteger resultCode, NSDictionary * info)
{
// do something with data
// return image
return image;
};
34. auto
id b = ^ UIImage * (NSData * result, NSUInteger resultCode, NSDictionary * info)
{
// do something with data
// return image
return image;
};
35. auto
id b = ^ UIImage * (NSData * result, NSUInteger resultCode, NSDictionary * info)
{
// do something with data
// return image
return image;
};
b(result, code, info); // compile time error!
36. auto
auto b = ^ UIImage * (NSData * result, NSUInteger resultCode, NSDictionary * info)
{
// do something with data
// return image
return image;
};
b(result, code, info); // everything is fine :)
41. Lambdas :: filter
vector<NSUInteger> v {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
auto const end = remove_if(v.begin(), v.end(), [](auto i)
{
return i % 2 == 0;
});
for (auto i = v.begin(); i != end; ++i)
{
NSLog(@"%@", @(*i));
}
// 1 3 5 7 9
42. Lambdas
[ capture ] ( params ) -> ret { body }
[] - capture nothing
[&] - capture all by reference
[=] - capture all by making copy
[&a] - capture a by reference
[a] - capture a by copy
43. Lambdas
typedef void (^Block) ();
@interface B : NSObject
@property (copy) Block b;
@end
@implementation B
- (void)foo
{
self.b = ^{
// do something with self
NSLog(@"%@", self);
};
}
@end
44. Lambdas
typedef void (^Block) ();
@interface B : NSObject
@property (copy) Block b;
@end
@implementation B
- (void)foo
{
__weak typeof(self) wself = self;
self.b = ^{
// do something with wself
NSLog(@"%@", wself);
};
}
@end