小编Rob*_*eph的帖子

单例在Swift 3中具有属性

在Apple的使用Swift with Cocoa和Objective-C文档(针对Swift 3更新)中,他们给出了以下Singleton模式的示例:

class Singleton {
    static let sharedInstance: Singleton = {
        let instance = Singleton()

        // setup code

        return instance
    }()
}
Run Code Online (Sandbox Code Playgroud)

让我们假设这个单例需要管理一个可变的字符串数组.如何/在哪里声明该属性并确保它被正确初始化为空[String]数组?

singleton swift3

87
推荐指数
3
解决办法
8万
查看次数

在Swift中使用NSTimer

在这种情况下,永远不会调用timerFunc().我错过了什么?

class AppDelegate: NSObject, NSApplicationDelegate {

    var myTimer: NSTimer? = nil

    func timerFunc() {
        println("timerFunc()")
    }

    func applicationDidFinishLaunching(aNotification: NSNotification?) {
        myTimer = NSTimer(timeInterval: 5.0, target: self, selector:"timerFunc", userInfo: nil, repeats: true)
    }
}
Run Code Online (Sandbox Code Playgroud)

nstimer swift

42
推荐指数
6
解决办法
5万
查看次数

在UIView的-drawRect:方法中绘制彩色文本

我试图在我的UIView子类中绘制彩色文本.现在我正在使用Single View应用程序模板(用于测试).除drawRect:方法外没有任何修改.

绘制文本但无论我将颜色设置为什么,它总是黑色的.

- (void)drawRect:(CGRect)rect
{
    UIFont* font = [UIFont fontWithName:@"Arial" size:72];
    UIColor* textColor = [UIColor redColor];
    NSDictionary* stringAttrs = @{ UITextAttributeFont : font, UITextAttributeTextColor : textColor };

    NSAttributedString* attrStr = [[NSAttributedString alloc] initWithString:@"Hello" attributes:stringAttrs];

    [attrStr drawAtPoint:CGPointMake(10.f, 10.f)];
}
Run Code Online (Sandbox Code Playgroud)

我也试过[[UIColor redColor] set]无济于事.

回答:

NSDictionary*stringAttrs = @ {NSFontAttributeName:font,NSForegroundColorAttributeName:textColor};

uikit nsattributedstring uicolor drawrect ios

24
推荐指数
1
解决办法
2万
查看次数

ActionController :: UrlGenerationError,没有路由匹配

我已经阅读了我能找到的每个类似的问题,但仍然无法弄清楚我的问题.

# routes.rb
Rails.application.routes.draw do
  resources :lists, only: [:index, :show, :create, :update, :destroy] do
    resources :items, except: [:new]
  end
end
Run Code Online (Sandbox Code Playgroud)
# items_controller.rb (excerpt)
class ItemsController < ApplicationController
  ...

  def create
    @list = List.find(params[:list_id])
    ...
  end
  ...
end
Run Code Online (Sandbox Code Playgroud)
# items_controller_spec.rb (excerpt)
RSpec.describe ItemsController, type: :controller do
   ...

  let!(:list) { List.create(title: "New List title") }

  let(:valid_item_attributes) {
    { title: "Some Item Title", complete: false, list_id: list.id }
  }

  let!(:item) { list.items.create(valid_item_attributes) }
  describe "POST #create" do
    context "with valid params" do
      it …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails rspec-rails

18
推荐指数
2
解决办法
2万
查看次数

从rspec控制器规范中访问控制器实例变量

我不应该能够在我的rspect测试中看到在控制器操作中创建的实例变量吗?

# /app/controllers/widget_controller.rb
...
def show
  @widget = ...
  puts "in controller: #{@widget}"
end
...
Run Code Online (Sandbox Code Playgroud)

-

# /spec/controllers/widget_controller_spec.rb
RSpec.describe WidgetController, type: :controller do
...
describe "GET #show" do
  it "assigns the requested widget as @widget" do
    get :show, { :id => 1 } # this is just an example - I'm not hardcoding the id

    puts "in spec: #{@widget}"
  end
end
...
Run Code Online (Sandbox Code Playgroud)

这是我运行该规范时得到的输出:

controller: #<Widget:0x007f9d02aff090>
in spec:
Run Code Online (Sandbox Code Playgroud)

我错误地认为我应该在我的控制器规范中访问@widget?

rspec ruby-on-rails rspec-rails

18
推荐指数
3
解决办法
9898
查看次数

Python中的运算符重载:处理不同类型和参数的顺序

我有一个简单的类,可以帮助对向量进行数学运算(即数字列表).我Vector可以乘以其他实例Vector 标量(floatint).

在其他更强类型的语言中,我将创建一个方法来将两个vectors和一个单独的方法相乘以乘以vectorint/ float.我仍然是Python的新手,我不确定如何实现它.我能想到的唯一方法是覆盖__mul__()并测试传入的参数:

class Vector(object):
  ...
 def __mul__(self, rhs):
  if isinstance(rhs, Vector):
     ...
  if isinstance(rhs, int) or isinstance(rhs, float):
    ...
Run Code Online (Sandbox Code Playgroud)

即使我这样做,我也会被迫乘以这样Vector的标量:

v = Vector([1,2,3])

result = v * 7
Run Code Online (Sandbox Code Playgroud)

如果我想在乘法中颠倒操作数的顺序怎么办?

result = 7 * v
Run Code Online (Sandbox Code Playgroud)

在Python中这样做的正确方法是什么?

python class operator-overloading operators

16
推荐指数
2
解决办法
3550
查看次数

Swift 3.0:如何调用CGImageCreateWithImageInRect()?

我需要在Swift 3.0中实现这个Objective-C代码(我正在使用Xcode 8 Beta 3):

// Note: this code comes from an Obj-C category on UIImage
CGImageRef imageRef = CGImageCreateWithImageInRect(self.CGImage, cropRect);
UIImage *image = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:self.imageOrientation];
Run Code Online (Sandbox Code Playgroud)

我在最新的文档中找不到任何内容CGImageCreateWithImageInRect().

cgimage swift swift3 xcode8-beta3

13
推荐指数
1
解决办法
8257
查看次数

如何实现__eq__进行集合包含测试?

我遇到了一个问题,我将一个实例添加到一个集合中,然后进行测试以查看该集合中是否存在该对象.我已经覆盖了__eq__()但是在包含测试期间它没有被调用.我必须改写__hash__()吗?如果是这样,我将如何实现,__hash__()因为我需要散列元组,列表和字典?

class DummyObj(object):

    def __init__(self, myTuple, myList, myDictionary=None):
        self.myTuple = myTuple
        self.myList = myList
        self.myDictionary = myDictionary

    def __eq__(self, other):
        return self.myTuple == other.myTuple and \
            self.myList == other.myList and \
            self.myDictionary == other.myDictionary

    def __ne__(self, other):
        return not self.__eq__(other)

if __name__ == '__main__':

    list1 = [1, 2, 3]
    t1    = (4, 5, 6)
    d1    = { 7 : True, 8 : True, 9 : True }
    p1 = DummyObj(t1, list1, d1)

    mySet = …
Run Code Online (Sandbox Code Playgroud)

python equality set

11
推荐指数
1
解决办法
6696
查看次数

模拟"拍照"屏幕闪烁

我有一个模拟拍照的AV基金会应用程序(如相机应用程序).通常以下情况适用于我,但不适用于此情况.

此代码在我的视图控制器中的操作中执行.它包含一个全屏UIView(videoPreviewLayer),它附有一个AVCaptureVideoPreviewLayer.动画执行但不显示任何内容.另请注意,我使用的是ARC,iOS 6,iPhone 4S,iPad3.

// Flash the screen white and fade it out
UIView *flashView = [[UIView alloc] initWithFrame:[[self videoPreviewView] frame]]; 
[flashView setBackgroundColor:[UIColor whiteColor]];
[[[self view] window] addSubview:flashView];

[UIView animateWithDuration:1.f
             animations:^{
                 [flashView setAlpha:0.f];
             }
             completion:^(BOOL finished){
                 [flashView removeFromSuperview];
             }
 ];
Run Code Online (Sandbox Code Playgroud)

以下是我如何附加AVCaptureVideoPreviewLayer:

 // Setup our preview layer so that we can display video from the camera
captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];

CALayer *viewLayer = videoPreviewView.layer;
viewLayer.masksToBounds = YES;

captureVideoPreviewLayer.frame = videoPreviewView.bounds;

[viewLayer insertSublayer:captureVideoPreviewLayer below:[[viewLayer sublayers] objectAtIndex:0]];
Run Code Online (Sandbox Code Playgroud)

注意经过进一步调查后,闪光灯会间歇性地发生.一般来说它似乎在它应该开始后大约5-10秒变得可见.我也看到它连续快速连续运行两次,即使我只调用一次代码.

animation avfoundation calayer uiview ios

10
推荐指数
2
解决办法
3020
查看次数

UITableView单元格中的图像不符合tintColor

我有一个UITableView由静态细胞组成的.在IB中,我将每个UITableViewCell人的风格设置为"基本"并设置图像(见截图).导航栏中的按钮表示tintColor属性,但tableview中的图像不符合.到目前为止,我已经在IB中完成了所有工作 - 如果我想要图像也尊重该属性,我是否必须使用代码tintColor

谢谢

在此输入图像描述

objective-c uitableview tintcolor ios

10
推荐指数
2
解决办法
4907
查看次数