我需要在闭包内使用self的弱引用.我为此目的使用以下代码:
func testFunction() {
self.apiClient.getProducts(onCompletion: { [weak self] (error, searchResult) in
self?.isSearching = false
}
}
Run Code Online (Sandbox Code Playgroud)
我可以在testFunction的主体中声明self的弱引用,而不是在关闭的捕获列表中给出弱引用.
func testFunction() {
weak var weakSelf = self
self.apiClient.getProducts(onCompletion: {(error, searchResult) in
weakSelf?.isSearching = false
}
}
Run Code Online (Sandbox Code Playgroud)
类似的,Objective-C中也使用了语法来使用块内的弱引用.
__weak typeof(self) weakSelf = self;
Run Code Online (Sandbox Code Playgroud)
在闭包中通过捕获列表指定弱引用是否有任何优势,而不是在函数体中声明弱变量.如果函数体中有多个闭包,则在函数体中声明弱变量并在所有闭包中使用相同的变量而不是在每个闭包中写入捕获列表更有意义.
我在我的Cell Subclass中有UILabel,假设有一个标题.标题的大小可以是各种长度,因此我需要调整UILabel的大小以适应文本,并防止文本不长,我还需要能够设置maxHeight.宽度应该相同.如何在tableViewCell子类中的swift中创建这样的代码?
到目前为止,我在awakeFromNib中有这个
theTitleLabel?.font = UIFont(name: "HelveticaNeue", size: 13)
theTitleLabel?.textColor = UIColor(rgba: "#4F4E4F")
theTitleLabel?.numberOfLines = 0
theTitleLabel?.lineBreakMode = NSLineBreakMode.ByTruncatingTail
Run Code Online (Sandbox Code Playgroud) 我真的被困在这里了.当我向下滚动UITableView时我想缩小导航栏,并在向上滚动时再次放大它.我设法改变导航栏的大小,但标题图像没有缩小导航栏.
我想完全像Safari一样,问题是我的TitleView的高度正在缩小,但宽度永远不会改变.
这是我用来获取滚动条高度的代码.
func scrollViewDidScroll(scrollView: UIScrollView) {
var navbar = navigationController?.navigationBar
var dir:CGPoint = tableview.panGestureRecognizer.translationInView(self.tableview)
var scrollViewHeight = tableview.frame.size.height
var scrollContentSizeHeight = tableview.contentSize.height
var scrollOffset = tableview.contentOffset.y
if (dir.y > 0 && self.formernavstate == "small") {
self.formernavstate = "big"
UIView.animateWithDuration(0.5, delay:0.0, options: UIViewAnimationOptions.AllowAnimatedContent, animations: { () -> Void in
println("")
navbar?.frame.origin.y = 20
self.navigationItem.titleView?.transform = CGAffineTransformMakeScale(0.52, 0.6)
}, completion: nil)
}
if (dir.y < 0 && self.formernavstate == "big") {
self.formernavstate = "small"
navbar?.frame.origin.y = 0
navigationItem.titleView?.transform = CGAffineTransformMakeScale(0.0001, 0.2) …Run Code Online (Sandbox Code Playgroud) 我正在尝试过滤Realm对象以查找createdAt日期少于一年前的对象:
let datex = NSCalendar.currentCalendar().startOfDayForDate(NSDate()).dateByAddingTimeInterval(-1*365*24*60*60)
let list = RealmDB.objects(TaskList).filter("createdAt <= '\(datex)'")
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
对于'TaskList'类型的对象上的属性'createdAt'的类型日期的预期对象,但收到:'2015-03-19 21:00:00 +0000'
我该如何解决?它看起来也像日期和时间值调整到时区.我怎样才能确定日期真的是一天的结束?
我很好奇,为什么这段代码可以正常工作而没有任何错误:
let a = [1]
print(a.index(after: a.endIndex)) // 2
Run Code Online (Sandbox Code Playgroud)
但是,如果我们尝试使用String类型重复此代码,则会收到错误消息:
let s = "a"
print(s.index(after: s.endIndex)) // Fatal error: Can't advance past endIndex
Run Code Online (Sandbox Code Playgroud)
根据Collection和Stringdocs,它们都有相同的陈述:
集合的有效索引。
i必须小于endIndex。
是一个错误还是一切正常?我正在使用Swift 4.2。
使用 SpriteKit,我如何平铺一个水平重复的 SKTexture 以填充 SKSpriteNode 的宽度?这就是我到目前为止所拥有的——仅拉伸纹理。
var header = SKSpriteNode()
let headerTexture = SKTexture(imageNamed: "inGameHeader-1.png")
header = SKSpriteNode(texture: headerTexture)
header.position = CGPoint(x: 0, y: CGRectGetMaxY(self.frame)-39)
header.size.width = CGRectGetWidth(self.frame)
header.size.height = 150
header.anchorPoint = CGPoint(x: 0,y: 1)
addChild(header)
Run Code Online (Sandbox Code Playgroud) 我尝试在Xcode 7游乐场中绘制sin wave.我有以下代码(来自这篇文章):
import XCPlayground
let sineArraySize = 64
let frequency1 = 4.0
let phase1 = 0.0
let amplitude1 = 2.0
let sineWave = (0..<sineArraySize).map {
amplitude1 * sin(2.0 * M_PI / Double(sineArraySize) * Double($0) * frequency1 + phase1)
}
func plotArrayInPlayground<T>(arrayToPlot:Array<T>, title:String) {
for currentValue in arrayToPlot {
XCPCaptureValue(title, value: currentValue)
}
}
plotArrayInPlayground(sineWave, title: "Sine wave 1")
Run Code Online (Sandbox Code Playgroud)
并Assistant Editor告诉我这个:
但我想要一个普通的图形(带轴等),如下所示:
我怎样才能实现这一目标?
我有以下课程:
class Category {
/**
* @ORM\OneToMany(targetEntity="Product", mappedBy="category")
*/
private $products;
...
}
class Product {
...
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
private $category;
...
}
Run Code Online (Sandbox Code Playgroud)
当我尝试从我的数据库中获取一个产品时,如下所示:
$query = $doctrineManager->createQuery(
"
SELECT p FROM AppBundle:Product p
WHERE p.id = :id
"
)->setParameter('id', $id);
$result = $query->getSingleResult();
Run Code Online (Sandbox Code Playgroud)
我不仅得到了我的product,还得到category了所有产品(除了我找到的产品).那么,如何在没有任何相关模型的情况下仅获取我想要的模型?
DispatchQueue.global(qos: .background).async {
//This will run on the background queue
self.writeValue(tag: GlobalData.WRITE_DATA, data: getDataForWrite(1) )
self.writeValue(tag: GlobalData.WRITE_DATA, data: getDataForWrite(2) )
self.writeValue(tag: GlobalData.WRITE_DATA, data: getDataForWrite(3) )
self.writeValue(tag: GlobalData.WRITE_DATA, data: getDataForWrite(4) )
self.writeValue(tag: GlobalData.WRITE_DATA, data: getDataForWrite(5) )
// .....
DispatchQueue.main.async {
//This will run on the main queue, after the previous code in outer block
print("done writing data")
}
}
Run Code Online (Sandbox Code Playgroud)
我需要停止从主线程执行此线程.这怎么可能
我有以下代码,我试图用它来初始化变量并对其执行一些操作。
let formattedPointsValue: String?
self.formattedPointsValue = model.pointUnitsEarned.stringValueWithWhiteSpaceThousandSeperator()+" "+"model.name".localized(in: .name) ?? .none
Run Code Online (Sandbox Code Playgroud)
但是我收到警告
nil 合并运算符 '??' 的左侧 具有非可选类型“String”,因此从不使用右侧。
当我删除?? .none我的项目时,我的项目运行良好,没有问题,但是当我运行单元测试时,我收到错误
致命错误:在解包可选值时意外发现 nil
我发现解决这个问题的唯一方法是使用这段代码。
if let unformattedValue = model.pointUnitsEarned {
self.formattedPointsValue = unformattedValue.stringValueWithWhiteSpaceThousandSeperator()+" "+"model.name".localized(in: .name)
} else {
self.formattedPointsValue = nil
}
Run Code Online (Sandbox Code Playgroud)
我想了解为什么这样的事情有效:
let legend: String?
self.legend = model.pointsCategory ?? .none
Run Code Online (Sandbox Code Playgroud)
但这失败了:
let formattedPointsValue: String?
self.formattedPointsValue = model.pointUnitsEarned.stringValueWithWhiteSpaceThousandSeperator()+" "+"model.name".localized(in: .name) ?? .none
Run Code Online (Sandbox Code Playgroud) swift ×9
ios ×5
closures ×1
doctrine-orm ×1
iphone ×1
navbar ×1
nsdate ×1
option-type ×1
php ×1
realm ×1
sprite-kit ×1
symfony ×1
textures ×1
tiling ×1
uilabel ×1
xcode7 ×1
xcplayground ×1