安装cocoapods后sudo gem install cocoapods
,尝试运行" pod setup
"会返回此错误:
Could not find 'cocoapods' (>= 0) among 64 total gem(s) (Gem::LoadError)
Run Code Online (Sandbox Code Playgroud)
"sudo gem install cocoapods"的输出:
CHANGELOG:
[CocoaPods](https://github.com/CocoaPods/CocoaPods/compare/0.28.0...0.29.0)
• [CocoaPods-core](https://github.com/CocoaPods/Core/compare/0.28.0...0.29.0)
• [cocoapods-downloader](https://github.com/CocoaPods/cocoapods-downloader/compare/0.2.0...0.3.0)
[ bug/enhancement notes removed for brevity]
Successfully installed cocoapods-0.29.0
Parsing documentation for cocoapods-0.29.0
1 gem installed
Run Code Online (Sandbox Code Playgroud)
当我列出安装的宝石时,我没有在我的本地宝石中看到cocoapods,但我确实看到/usr/local/Cellar/ruby/2.0.0-p0/bin中列出了'pod'.
系统配置:
Mac OS 10.9 (upgraded from 10.8)
Xcode: 5.0.2 (upgrade from 4.x)
Ruby path: /usr/local/Cellar/ruby/2.0.0-p0
Run Code Online (Sandbox Code Playgroud)
我怎样才能让cocopods为我工作?
在Swift中工作,我想将枚举(Int类型)转换为NSNumber并返回.我可以从枚举转换为数字,但我无法转换回来.推荐的方法是什么?
enum UpdateMode: Int {
case Undefined = 0,
Daily,
Weekly,
Monthly
}
var mode = UpdateMode.Weekly
var num: NSNumber = mode.rawValue // this works
// error: 'Int32' is not convertible to 'UpdateMode'
var convertedMode = num.integerValue as UpdateMode
Run Code Online (Sandbox Code Playgroud) 我想在我的应用程序中进行状态恢复,不使用故事板.我看到我的主要应用程序ViewController在状态恢复期间实例化了两次 - 你如何确保它只创建一次?
就我了解的流动,application:willFinishLaunchingWithOptions
并且pplication:didFinishLaunchingWithOptions
将使用commonInit
这将设置应用程序的UIWindow方法及其RootViewController的.在我的例子中,rootViewController是一个UINavigationController,其中一个名为"MyMainViewController"的类充当UINavigation的rootViewController.
除此之外,我也分别处理willEncodeRestorableStateWithCoder
和处理didDecodeRestorableStateWithCoder
.但似乎到了我的时候didDecodeRestorableStateWithCoder
,我看到MyMainViewController创建了两个独立的实例.
确保在恢复期间只创建一个UIViewController的方法是什么?
恢复期间的呼叫顺序:
这是我在AppDelegate中所做的事情:
NSString * const kRootViewControllerKey = @"RootViewControllerKey";
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self commonInitWithOptions:launchOptions];
return YES;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self commonInitWithOptions:launchOptions];
return YES;
}
- (void)commonInitWithOptions:(NSDictionary *)launchOptions {
static dispatch_once_t predicate;
dispatch_once(&predicate, ^ {
// While this will be called only once during the lifetype of the app, when the process is killed …
Run Code Online (Sandbox Code Playgroud) 在雨燕语种导游向您展示如何创建采取可变参数的函数。它还注意到参数被收集到一个数组中。
因此,如果我有示例函数 sumOf,如指南中所述:
func sumOf(numbers: Int...) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
Run Code Online (Sandbox Code Playgroud)
第二个函数也接受可变数量的参数,如何将相同的 var_args 值从第二个传递到第一个?
func avgOf(numbers: Int...) -> Int {
// Can't do this without the complier complaining about:
// "error: could not find an overload for '__conversion' that accepts the supplied arguments"
var sum = sumOf(numbers)
var avg = sum / numbers.count
return avg
}
Run Code Online (Sandbox Code Playgroud) 我遇到了一些代码,这些代码翻转了如何检查条件,并想知道为什么除了一个奇怪的个人怪癖之外还要做这件事.我从未见过任何教科书使用它,也没有看到任何样本代码以这种方式完成.
// why do it this way?
if (5 == myValue)
{
// do something
}
// instead of:
if (myValue == 5)
{
// do something
}
Run Code Online (Sandbox Code Playgroud)
我只是为==操作数看到了这种方式,但没有看到任何其他操作数.
我在没有rails/rspec背景的情况下进入并尝试在iOS上进行单元测试的'specta'框架.我不明白的一件事是何时使用specta的'sharedExamplesFor'.
它只是在所有测试套件中共享的测试,您可以在每个测试之前运行,这是一组类似测试用例的一部分吗?
初始化 CBCentralManager 实例(需要委托并且通常指向所属类)的好方法是什么?
我可以将该属性声明为隐式展开的可选属性,但作为一般做法这样做似乎不太像 Swift,也不是很安全。
或者,我可以将该属性声明为可选属性。但由于 CBCentralManager 的初始值设定项未声明为可失败,因此将实例声明为可失败似乎没有意义。
隐式解包可选:
class MyUnwrappedOptional: NSObject, CBCentralManagerDelegate {
var centralManager: CBCentralManager!
func init() {
super.init()
centralManager = CBCentralManager(delegate: self, queue: nil, options:nil)
// Subsequent usages of centralManager in other methods of this class don't require additional unwrapping.
centralManager.scanForPeripheralsWithServices(services, options: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
使用可选:
class MyOptionalClass: NSObject, CBCentralManagerDelegate {
var centralManager: CBCentralManager?
func init() {
super.init()
centralManager = CBCentralManager(delegate: self, queue: nil, options:nil)
// Subsequent usages of centralManager in other methods of this class require …
Run Code Online (Sandbox Code Playgroud) 每当我在我的"主"分支的功能分支中进行变基础时,我的功能分支似乎丢失了它的跟踪信息.这个工作流程出了什么问题?
$ git clone repo_url
$ git co -b feature/my_work_task
Run Code Online (Sandbox Code Playgroud)
在这里做一堆工作
$ git commit -am"Commit my work"
$ git push # push the branch upstream
$ git checkout master
$ git pull # get whatever changes that have been put into master
$ git co feature/my_work_task
$ git rebase master # everything seems to be good
Run Code Online (Sandbox Code Playgroud)
在我的功能分支中进行一些其他更改.
$ git commit -am"more changes to update"
$ git push # pushing this to my remote repo
Run Code Online (Sandbox Code Playgroud)
推送到远程仓库失败,出现以下错误:
To git@github.com:/username/my-repo.git
! [rejected] HEAD …
Run Code Online (Sandbox Code Playgroud) 我想在NSString中使用Unicode版本的'<',但编译器会产生错误:
"Character '<' cannot be specified by a universal character name"
Run Code Online (Sandbox Code Playgroud)
我用的时候:
NSString *text = @"Some Text: \u003C";
Run Code Online (Sandbox Code Playgroud)
'<'似乎是一个特殊的字符,以及"="和其他一些字符,所以什么是在字符串中插入文字'<'的方法,而不是'<'
像这样使用:"some string <"
?
我没有控制字符串值本身和上面的值作为内联用于演示目的.
C99指定初始化器或各种CGSizeMake,CZRectMake等宏是否更像现代Objective-C中的惯例?
它几乎看起来像个人风格偏好,但我看到的C99风格的一个优点是价值的意图是清晰明确的.
CGRect rect = CGRectMake(x, y, width, height)
Run Code Online (Sandbox Code Playgroud)
如果你混淆了价值的顺序,会给你带来心痛,如:
CGRect rect = (CGRect){.origin.x = x, .origin.y = y, .size.width = width, .size.height = height};
Run Code Online (Sandbox Code Playgroud)
毫无疑问,高度正在获得你给予它的价值.人们继续使用现有宏的原因是什么?
"可可编码公约"的文档中没有任何关于此的内容和我遇到的风格指南之一,它是GitHub风格指南:https://github.com/github/objective-c-conventions
如果我在32位平台上声明了int类型的值并执行以下操作:
int32_t mask;
mask = 1 << 31: // produces 2147483648 (or 0x80000000)
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我理解为什么上述行会产生警告:
The result of the '<<' expression is undefined
Run Code Online (Sandbox Code Playgroud) 我正在学习自动布局,我想设置一组垂直堆叠并均匀分布的按钮.我也喜欢固定在视图底部的按钮.使用VFL设置这些约束的好方法是什么?按钮列表将作为UIButtons数组传入.
NSArray *buttons = [button1, button2, button3, button4, ...]
NSMutableArray *allConstraints = [NSMutableArray array]
UIButton *previousButton;
for (UIButton button in buttons) {
// Buttons take up full width
NSArray *constraints = [NSLayoutConstraint
constraintsWithVisualFormat:@"H:|[button]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(button);];
[allConstraints addObjectsFromArray:constraints];
constraints = [NSLayoutConstraint
constraintsWithVisualFormat:@"V:|[button]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(button);];
[allConstraints addObjectsFromArray:constraints];
if (!previousButton) {
NSDictionary *metrics = @{@"padding" : @(10)};
// Make buttons height
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[-(padding)-previousButton(==button)]"
options:0
metrics:metrics
views:NSDictionaryOfVariableBindings(previousButton, button)];
[allConstraints addObjectsFromArray:constraints];
}
previousButton = button;
}
[self.view addConstraints:allConstraints]
Run Code Online (Sandbox Code Playgroud)
这不能实现我所需要的,因为按钮没有固定到视图的底部.
ios ×4
c++ ×2
objective-c ×2
swift ×2
autolayout ×1
c ×1
c99 ×1
cocoapods ×1
enums ×1
git ×1
github ×1
initializer ×1
swift2 ×1
unicode ×1
unit-testing ×1