小编MCM*_*tan的帖子

索引字符串中的上标美分

我试图让我的标签看起来像这样:

在此输入图像描述

但是使用属性字符串,我设法得到了这个结果:

在此输入图像描述

我的代码:

NSString *string = [NSString stringWithFormat:@"%0.2f",ask];

NSMutableAttributedString *buyString = [[NSMutableAttributedString alloc] initWithString:string];

[buyString addAttribute:NSFontAttributeName
                  value:[UIFont systemFontOfSize:15.0]
                  range:NSMakeRange(2, buyString.length - 2)];

self.labelBuy.attributedText = buyString;
Run Code Online (Sandbox Code Playgroud)

如你所见,点后的数字,保持在下面,我想将它们作为第一个例子弹出到顶部.有没有办法设置属性字符串框架?

objective-c nsattributedstring

3
推荐指数
1
解决办法
2304
查看次数

Cocoapods,以通用方式向所有目标添加依赖项

App有20多个目标.

每次添加目标时,我都需要更新我的podfile:

Podfile

target 'SomeNewTarget' do
    runAllPods // Block that contains all "pod ...."
end
Run Code Online (Sandbox Code Playgroud)

我的问题:

是否有一种向所有目标添加依赖项的通用方法?我基本上希望向除测试目标之外的所有目标添加相同的依赖项.

完整的podifle:

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

inhibit_all_warnings!

def runAllPods
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for MyApp

  pod 'Swinject', '~> 2.1.0'
  pod 'SwinjectAutoregistration', '2.1.0'
  pod 'IHKeyboardAvoiding', '4.0'
  pod 'SnapKit', '3.2.0'
  pod 'Material', '~> 2.10.2'
  pod 'RxSwift', '~> …
Run Code Online (Sandbox Code Playgroud)

dependency-management ios cocoapods swift swift-package-manager

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

当其他属性发生变化时如何更新一个属性

我是 Rails 新手,可以正确解决这个问题。

当“completed”变为 true 时尝试更新“completed_at”。

我读过这个: https: //api.rubyonrails.org/classes/ActiveModel/Dirty.html#method-i-changed-3F

并决定在每次更新或保存时添加回调。然后检查“completed”属性是否已更改,如果没有更改,如果为 true,则将“completed_at”更新为当前日期。

方法“check_is_completed”确实被调用,但我无法访问正确的道具并检测是否更改并获取新值

class Task < ApplicationRecord
  belongs_to :user
  validates :name, presence: true

  after_save :check_is_completed
  after_update :check_is_completed

  private 

  # This gets called
  def check_is_completed
    # try 1
    # This does not work called
    if @completed.changed?
      if @completed == true
      self.completed_at = Date.new()
      end
    end
    #try 2
    if self.completed_changed?
      if self.completed == true
      self.completed_at = Date.new()
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails ruby-on-rails-5

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

Swift使用NSTimer导致'deinit'不调用

使用NSTimer之后,将永远不会调用类deinit方法。该类是用Swift编写的,我在“ init”处调用计时器,如下所示:

init(eventsDistributor : EventsDistributor, orderSide : ORDER_SIDE, stockViewModel : StockViewModel, orderType : ORDER_TYPE_ENUM, orderPrice : NSNumber) {
    self.eventsDistributor = eventsDistributor
    self.orderSide = orderSide
    self.stockViewModel = stockViewModel
    self.orderType = orderType
    self.orderPrice = orderPrice
    super.init()
    _events()
    loadPreOrderDetails()

    //Here I call the Timer:
    var reloadTimer = NSTimer.scheduledTimerWithTimeInterval(10.0, target: self, selector: "loadSecurityTradeInfo", userInfo: nil, repeats: true)
    reloadTimer.fire()        
}
Run Code Online (Sandbox Code Playgroud)

我也尝试将NSTimer用作局部变量,但在该处没有成功...有人遇到此问题吗?我可以在Swift中使用一个不会导致不取消分配类的计时器吗?谢谢

nstimer swift deinit

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

使类类型为Dictionary键(Equatable,Hashable)

假设我有一个名为LivingCreature And的其他类继承自它的类:

  • Human

  • Dog

  • Alien

这就是我想要完成的事情:

let valueForLivingCreature = Dictionary<Alien, String>
Run Code Online (Sandbox Code Playgroud)

并像这样访问它:

let alienValue = livingCreatureForValue[Alien]
Run Code Online (Sandbox Code Playgroud)

但这意味着类应该符合EquatableHashable,但类本身,而不是类实例.我已经尝试了各种方法来实现这一点,但没有运气.

作为妥协,我想出的是:

typealias IndexingValue = Int
class LivingCreature {
     static var indexingValue: IndexingValue = 0 
}
Run Code Online (Sandbox Code Playgroud)

然后我可以像这样使用类作为键:

 let livingCreatureForValue = Dictionary<IndexingValue, String>
Run Code Online (Sandbox Code Playgroud)

访问:

let alienValue = livingCreatureForValue[Alien.indexingValue]
Run Code Online (Sandbox Code Playgroud)

但是,这样就应该手动为每个类设置IndexingValue.我想像这样从类本身做一个哈希:

class LivingCreature {
    static var indexingValue: IndexingValue {
        return NSStringFromClass(self).hash
    }
}
Run Code Online (Sandbox Code Playgroud)

这是不可能的,因为self无法访问静态var.

我的问题是,有没有更好的方法来解决这类问题?

编辑:

@Paulw11问我为什么不让LivingCreature确认Equatable和Hashable,原因是我无法通过类类型引用访问该值.我每次都要分配一个实例:

let alienValue = livingCreatureForValue[Alien()]
Run Code Online (Sandbox Code Playgroud)

我不想每次都找"Alien()"来寻找价值.而使用它的组件,不关心livingCreature实例,只关心类类型.

ios swift

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