问题
我创建了一个带有自定义UICollectionViewCell的UICollectionViewController.自定义单元格包含一个大的矩形UIView(名为colorView)和一个UILabel(名为nameLabel).
首次使用单元格填充集合并打印colorView.frame时,打印的帧具有不正确的值.我知道它们是不正确的,因为colorView帧大于单元框架本身,即使colorView被正确绘制.
但是,如果我滚动collectView足以触发重用以前创建的单元格,colorView.frame现在具有正确的值!我需要正确的帧,因为我想将圆角应用于colorView图层,我需要正确的coloView大小才能执行此操作.顺便说一下,如果你想知道,colorView.bounds也有与colorView.frame相同的错误大小值.
这个问题
创建单元格时为什么帧不正确?
现在一些代码
这是我的UICollectionViewCell:
class BugCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var colorView: UIView!
@IBOutlet weak var nameLabel: UILabel!
}
Run Code Online (Sandbox Code Playgroud)
这是UICollectionViewController:
import UIKit
let reuseIdentifier = "Cell"
let colors = [UIColor.redColor(), UIColor.blueColor(),
UIColor.greenColor(), UIColor.purpleColor()]
let labels = ["red", "blue", "green", "purple"]
class BugCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return colors.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> …Run Code Online (Sandbox Code Playgroud) 我有一个UILabel,文本在故事板中设置为"归属".当我生成Main.strings文件以翻译成另一种语言时,不会出现此标签的文本.
我试图通过复制对象id手动将一个条目添加到Main.strings文件中.我尝试设置"text"属性和"attributedText"属性,但是当我运行应用程序时,不使用已翻译的字符串.
那么,如何将UILabel集本地化为故事板上的"归属"?
我创建了一个包含数组的类.我在视图控制器中向该数组添加了一个观察者,并对该数组执行了一些修改.
问题是,当我打印observeValueForKeyPath()方法返回的更改字典时,我只能看到类型NSKeyValueChangeSetting的更改.换句话说,该方法告诉我数组已更改,向我提供旧的和新的数组(包含所有元素)但我想收到添加或删除的特定项的信息.
这是一些示例代码.
这是将观察其数组的类.
private let _observedClass = ObservedClass()
class ObservedClass: NSObject {
dynamic var animals = [String]()
dynamic var cars = [String]()
class var sharedInstance: ObservedClass {
return _observedClass
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的视图控制器的代码.
class ViewController: UIViewController {
var observedClass = ObservedClass.sharedInstance
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
observedClass.addObserver(self, forKeyPath: "animals", options: .New | .Old, context: nil)
}
deinit {
observedClass.removeObserver(self, forKeyPath: "animals")
}
override func viewDidLoad() {
super.viewDidLoad()
observedClass.animals.insert("monkey", atIndex: 0)
observedClass.animals.append("tiger")
observedClass.animals.append("lion")
observedClass.animals.removeAtIndex(0)
}
override func observeValueForKeyPath(keyPath: String, …Run Code Online (Sandbox Code Playgroud) 我正在尝试访问位于64位处理器PC中"c:/ Program Files(x86)"文件夹中的dll.
如果我使用os.path.exists来检查dll是否存在,我会得到一个肯定的答案:
>>> print os.path.exists('c:/Program Files (x86)/Some Folder/SomeDll.dll')
True
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用ctypes加载dll时,我收到以下错误:
>>> from ctypes import WinDLL
>>> some_dll = WinDLL('c:/Program Files (x86)/Some Folder/SomeDLL.dll')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python26\lib\ctypes\__init__.py", line 353, in __init__
self._handle = _dlopen(self._name, mode)
WindowsError: [Error 126] The specified module could not be found
Run Code Online (Sandbox Code Playgroud)
在32位PC中,dll位于"c:/ Program Files"文件夹中,我可以毫无问题地打开它.我想也许问题是文件夹名称中是否存在括号.由于返回的异常是WindowsError,它似乎是负责加载库的操作系统函数中的一个缺陷.
所以,问题是:如何加载位于"c:/ Program Files(x86)"文件夹中的dll?我无法将dll复制到另一个目的地,它必须位于原始路径中...
谢谢!
很难从Apple找到适用于UI测试框架的文档.我已经看到很多可以找到具有特定名称的按钮和标签的例子,但我怎样才能找到通用的UIViews?
例如,假设我使用accessibilityLabel =="Some View"设置视图,如何在测试执行期间找到具有该名称的视图?
我试过(不成功)以下代码:
XCUIElement *pvc = [[app childrenMatchingType:XCUIElementTypeAny] elementMatchingPredicate:[NSPredicate predicateWithFormat:@"accessibilityLabel == 'Places View'"]];
NSPredicate *exists = [NSPredicate predicateWithFormat:@"exists == true"];
[self expectationForPredicate:exists evaluatedWithObject:pvc handler:nil];
[self waitForExpectationsWithTimeout:10 handler:nil];
NSLog(@"%@", [[app childrenMatchingType:XCUIElementTypeAny] elementMatchingPredicate:[NSPredicate predicateWithFormat:@"accessibilityLabel == 'Places View Controller'"]]);
Run Code Online (Sandbox Code Playgroud) 我一直在使用 Firebase 函数外壳 (firebase Experimental:functions:shell),我发现它非常适合测试 firebase 数据库和 http 侦听器。
但是,我必须在 shell 中复制粘贴测试代码,这有点烦人。
有什么办法可以在文件中编写一些 js 代码,然后使用 Firebase shell 运行完整的文件?不用复制粘贴?
firebase firebase-realtime-database google-cloud-functions firebase-cli
我正在生成要发送到 AppStore 的应用程序预览视频。根据设备类型,AppStore 需要特定的视频分辨率。
我有一个主视频,分辨率为 1920x1080,我可以上传。
我需要生成另一个分辨率为 1920x886 的视频以支持 6.5" 设备,因此我使用以下命令执行缩放:
ffmpeg -i video_1920_1080.mp4 -vf scale=1920:886 -c:a copy video_1920_886.mp4
Run Code Online (Sandbox Code Playgroud)
如果我使用 ffmpeg -i 获取生成的视频的信息,我会得到以下结果:
ffmpeg -i video_1920_886.mp4
ffmpeg version 3.4.2 Copyright (c) 2000-2018 the FFmpeg developers
built with Apple LLVM version 9.0.0 (clang-900.0.39.2)
configuration: --prefix=/usr/local/Cellar/ffmpeg/3.4.2 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --disable-jack --enable-gpl --enable-libmp3lame --enable-libx264 --enable-libxvid --enable-opencl --enable-videotoolbox --disable-lzma
libavutil 55. 78.100 / 55. 78.100
libavcodec 57.107.100 / 57.107.100
libavformat 57. 83.100 / 57. 83.100
libavdevice 57. 10.100 / …Run Code Online (Sandbox Code Playgroud) 我一直在为这个问题苦苦挣扎,但编码工作非常痛苦,我必须向您的聪明人寻求帮助。
在我去乌克兰的一次旅行中,一位朋友将一些乌克兰命名的文件复制到我的笔式驱动器中。但是,正如您所料,在复制到我的计算机的过程中,文件名变得无法读取垃圾,例如:
Ôàíòîì
Run Code Online (Sandbox Code Playgroud)
好吧,我有充分的理由相信原始文件名是使用 CP1251 编码的(我知道这一点是因为我手动检查了编码表并设法正确翻译了乐队的名称)。显然发生的事情是,在复制过程中,CP1251 代码被维护,操作系统现在只是将它们解释为 Unicode 代码。
我尝试使用以下脚本“解释”Python 中的代码:
print u"Ôàíòîì".decode('cp1251')
Run Code Online (Sandbox Code Playgroud)
不过感觉不太对。结果也完全是垃圾:
?”? ????®?¬
Run Code Online (Sandbox Code Playgroud)
如果我做:
print repr(u"Ôàíòîì".decode('cp1251'))
Run Code Online (Sandbox Code Playgroud)
我获得:
u'\u0413\u201d\u0413\xa0\u0413\xad\u0413\u0406\u0413\xae\u0413\xac'
Run Code Online (Sandbox Code Playgroud)
我发现,如果我能得到 Unicode 中的所有代码点,并将它们偏移 0x350,我会将它们放置在乌克兰西里尔文的正确位置。但我不知道该怎么做,可能有一个答案在概念上比这更正确。
任何帮助将不胜感激!
编辑:这是正确翻译的示例
Ôàíòîì 应该翻译成??????。
Ô 0x00D4 -> ? 0x0424
à 0x00E0 -> ? 0x0430
í 0x00ED -> ? 0x043D
ò 0x00F2 -> ? 0x0442
î 0x00EE -> ? 0x043E
ì 0x00EC -> ? 0x043C
Run Code Online (Sandbox Code Playgroud)
正如我之前所说,正确和错误的代码点之间有一个 0x0350 偏移量。
(好吧,文件是音乐文件......我猜你怀疑......)
其他一些测试字符串(我不知道其翻译):Áåç êîíò›îë?Äâîº Êàï_òîøêà Ïîäèâèñü
我在ReactNative项目上开玩笑。我想在一个测试用例中比较两个相同类的对象。这是一个示例类定义:
class Person {
constructor(id, name, lastName) {
this.id = id;
this.name = name;
this.lastName = lastName;
}
fullName = () => {
return `${this.name} ${this.lastName}`;
}
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个测试用例,该用例比较了Person应该相同的类的两个对象:
test('checks the Person.constructor method', () => {
expect(new Person(1, 'John', 'Smith')).toEqual(new Person(1, 'John', 'Smith'));
});
Run Code Online (Sandbox Code Playgroud)
但是我得到以下结果:
FAIL __tests__/Comparison-test.js (7.328s)
? checks the Person.constructor method
expect(received).toEqual(expected)
Expected: {"fullName": [Function anonymous], "id": 1, "lastName": "Smith", "name": "John"}
Received: {"fullName": [Function anonymous], "id": 1, "lastName": "Smith", "name": "John"}
48 |
49 | …Run Code Online (Sandbox Code Playgroud) 在Xcode 6.3.1上,我为我的应用程序创建了一个启动屏幕,该应用程序具有相同的初始ViewController.
问题是,当应用程序启动时,所有字体的大小都略有不同.
我在标签上使用标题和标题1文字样式.
下面有一些图片可以让问题更加清晰.
谢谢!
启动屏幕:
主故事板初始视图控制器:
叠加两个图像时,差异很明显:
在我的代码中,我在类中创建了一个 CLLocationManager 属性。我想使用闭包来初始化位置管理器,因为我可以在那里设置委托,而且我认为在其声明上配置属性非常棒(就像我以前在 Objective C 中的属性 getter 中所做的那样)。
所以我尝试了这个:
class MapViewController: UIViewController {
var locationManager: CLLocationManager = {
var lm = CLLocationManager()
lm.delegate = self
return lm
}()
extension MapViewController: CLLocationManagerDelegate {
}
Run Code Online (Sandbox Code Playgroud)
但我在 lm.delegate = self 行上收到以下错误。
/Users/.../MapViewController.swift:18:23:无法将“NSObject -> () -> MapViewController”类型的值分配给类型“CLLocationManagerDelegate”?
MapViewController 符合委托(因为下面的扩展),所以我认为“非确认”不是问题。
我知道,我可以在 viewDidLoad 上初始化委托,但如果可以在属性本身上初始化,我真的很想学习这样做,因为它感觉很简洁。
谢谢!
我的应用程序有一个导航控制器,其中第一个视图控制器是一个登录屏幕,所有其他屏幕都位于顶部。
每当后端的任何HTTP请求返回401错误时,我都想退回到登录屏幕。
我想到的是使用以下内容向ViewController类添加扩展:
extension UIViewController {
func unwindToLoginScreen() {
performSegue(withIdentifier: loginScreen)
}
}
Run Code Online (Sandbox Code Playgroud)
这场比赛将是一场放松的比赛。然后,每当请求失败时,我都会调用视图控制器的unwindToLoginScreen方法。
但是,这种方法的问题在于,我必须记住要为添加到项目中的所有新视图控制器在情节提要上创建所述展开序列。
到目前为止,我认为理想的计划应该是能够以编程方式创建展开的序列,而不是使用情节提要。因此,unwindToLoginScreen()默认情况下,我的扩展方法可在任何新的视图控制器中使用。就像是:
extension UIViewController {
func unwindToLoginScreen() {
let segue = UnwindSegue(identifier: "blablah", segue: LoginViewController.unwindToLoginViewController)
segue.perform()
}
}
Run Code Online (Sandbox Code Playgroud)
有可能做这样的事情吗?
ios ×6
swift ×4
objective-c ×2
python ×2
xcode ×2
app-preview ×1
arrays ×1
closures ×1
cp1251 ×1
ctypes ×1
decode ×1
dll ×1
encode ×1
ffmpeg ×1
firebase ×1
firebase-cli ×1
iphone ×1
javascript ×1
jestjs ×1
localization ×1
macos ×1
react-native ×1
unicode ×1
video ×1