我试图找出一种方法来typeof创建一个弱的引用,self以便在块中使用,以避免保留周期.
当我第一次读到这个时,似乎是使用惯例__block typeof(self) bself = self;,编译但是使用__block以避免保留周期不再起作用而__weak应该使用.
但是会__weak typeof(self) bself = self;导致错误:
类型'typeof(self)'(又名'TUAccountsViewController*const __strong')已经设置了保留属性
有没有办法使用typeof或其他调用一般创建一个弱引用self?
cocoa weak-references objective-c objective-c-blocks automatic-ref-counting
我想弄清楚如何在Qt中制作一条水平线.这很容易在Designer中创建,但我想以编程方式创建一个.我做了一些googleing并查看了ui文件中的xml但却无法解决任何问题.
这是ui文件中的xml:
<widget class="Line" name="line">
<property name="geometry">
<rect>
<x>150</x>
<y>110</y>
<width>118</width>
<height>3</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
Run Code Online (Sandbox Code Playgroud) 我正在用钱输入屏幕,需要实现一个自定义项,init以根据初始化的金额设置状态变量。
我以为这可以用,但是出现编译器错误:
Cannot assign value of type 'Binding<Double>' to type 'Double'
struct AmountView : View {
@Binding var amount: Double
@State var includeDecimal = false
init(amount: Binding<Double>) {
self.amount = amount
self.includeDecimal = round(amount)-amount > 0
}
...
}
Run Code Online (Sandbox Code Playgroud) 看看NS_INLINE它的定义似乎使用它的优点static inline是编译器兼容性,这是正确的吗?在objective-c项目中应该NS_INLINE总是使用而不是static inlinec函数吗?
#if !defined(NS_INLINE)
#if defined(__GNUC__)
#define NS_INLINE static __inline__ __attribute__((always_inline))
#elif defined(__MWERKS__) || defined(__cplusplus)
#define NS_INLINE static inline
#elif defined(_MSC_VER)
#define NS_INLINE static __inline
#elif TARGET_OS_WIN32
#define NS_INLINE static __inline__
#endif
#endif
Run Code Online (Sandbox Code Playgroud) 我正在尝试设置一个视图,该视图可以显示多个模式,具体取决于所点击的按钮。
当我只添加一个时sheet,一切正常:
.sheet(isPresented: $showingModal1) { ... }
Run Code Online (Sandbox Code Playgroud)
但是,当我添加另一张纸时,只有最后一张可用。
.sheet(isPresented: $showingModal1) { ... }
.sheet(isPresented: $showingModal2) { ... }
Run Code Online (Sandbox Code Playgroud)
更新
我试图使此工作正常,但是我不确定如何为声明类型modal。我收到的错误Protocol 'View' can only be used as a generic constraint because it has Self or associated type requirements。
struct ContentView: View {
@State var modal: View?
var body: some View {
VStack {
Button(action: {
self.modal = ModalContentView1()
}) {
Text("Show Modal 1")
}
Button(action: {
self.modal = ModalContentView2()
}) {
Text("Show Modal 2")
} …Run Code Online (Sandbox Code Playgroud) 有没有人知道一个库或什么会将NSDate转换成字符串,如下面的例子?
1 hour ago
yesterday
last Thursday
2 days ago
last month
6 months ago
last year
Run Code Online (Sandbox Code Playgroud) 我已经看到一些帖子推荐isinstance(obj, collections.Sequence)而不是hasattr(obj, '__iter__')确定某些内容是否是列表.
len(object)或hasattr(object,__iter__)?
起初我很兴奋,因为测试一个物体__iter__对我来说总是很脏.但经过进一步审查后,这似乎仍然是最好的解决方案,因为没有一个isinstance测试collection产生相同的结果.collections.Sequence是接近但它返回True字符串.
hasattr(obj, '__iter__')
set([]): True
{}: True
[]: True
'str': False
1: False
isinstance(obj, collections.Iterable)
set([]): True
{}: True
[]: True
'str': True
1: False
isinstance(obj, collections.Iterator)
set([]): False
{}: False
[]: False
'str': False
1: False
isinstance(obj, collections.Sequence)
set([]): False
{}: False
[]: True
'str': True
1: False
Run Code Online (Sandbox Code Playgroud)
这是我用来生成这个的代码:
import collections
testObjs = [
set(),
dict(),
list(),
'str',
1
] …Run Code Online (Sandbox Code Playgroud) 我想在没有默认动画的情况下设置a的strokeEnd属性CAShapeLayer,根本没有动画.我环顾四周试图找到如何做到这一点,但一切似乎都是关于如何动画属性.
有谁知道一个库确定是否可以使用pushState?
我在用这个:
if(window.history.pushState){
window.history.pushState(null, document.title, path);
}else{
location.pathname = path;
}
Run Code Online (Sandbox Code Playgroud)
但我刚刚发现Safari 5.0.2中存在一个错误,即使上述测试通过,也会导致它无法正常工作:http://support.github.com/discussions/site/2263-line-links-broken.
我想可能还有其他陷阱,有人可能已经发现它们并将它们包裹起来但我还没有找到任何东西.
编辑: @Crescent Fresh
从我所看到的,似乎pushState推送到历史堆栈并更改URL但不更新location.pathname.在我的代码中,我使用setInterval来检查路径是否已更新.
var cachedPathname = location.pathname;
if(window.history.pushState){
cachedPathname = location.pathname;
setInterval(function(){
if(cachedPathname !== location.pathname){
cachedPathname = location.pathname;
//do stuff
}
}, 100);
}
Run Code Online (Sandbox Code Playgroud)
在Safari 5.0.2中,当pushState更改url时,location.pathname不会更改.这适用于其他浏览器和Safari版本.
我正在使用PyQt并试图在QtDesigner中推广一个小部件.如果我在"头文件"字段中指定包含我的窗口小部件子类的文件的完整模块路径,我能够使它工作.
有没有更好的方法将QtDesigner中的窗口小部件提升为PyQt窗口小部件而无需指定完整的模块路径?
这是一个有希望说明我在说什么的例子:
/PythonModuleRoot/Ui/MainUi.py
from PyQt4 import QtCore, QtGui, uic
class MainUi(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.ui = uic.loadUi(os.path.join(os.path.dirname(__file__), 'MainUi.ui'), self)
Run Code Online (Sandbox Code Playgroud)
/PythonModuleRoot/Ui/CustomWidget.py
from PyQt4 import QtCore, QtGui, uic
class CustomWidget(QtGui.QWidget):
def __init__(self, parent):
QtGui.QWidget.__init__(self, parent)
Run Code Online (Sandbox Code Playgroud)
/PythonModuleRoot/Ui/MainUi.ui
在MainUi.ui中,我提升了一个小部件并将Header文件字段设置为:"PythonModuleRoot.Ui.CustomWidget".