如何解决从WatchOS捕获Apple Watch的型号标识符
似乎没有任何记录的官方方法可以从手表应用程序获取Apple Watch模型,但是这篇文章显示了sysctlbyname
的特殊用法:
How to determine the Apple Watch model?
有人可以帮我弄清楚为什么此功能不起作用吗?有时会返回
“ Watc \ t”
而不是预期的
“ Watch2,4”
Swift的sysctlbyname函数似乎是这里描述的c函数: https://opensource.apple.com/source/Libc/Libc-167/gen.subproj/sysctlbyname.c.auto.html
我的代码是从SO帖子的快速答案中复制的:
private func getWatchModel() -> String? {
var size: size_t = 0
sysctlbyname("hw.machine",nil,&size,0)
var machine = CChar()
sysctlbyname("hw.machine",&machine,0)
return String(cString: &machine,encoding: String.Encoding.utf8)?.trimmingCharacters(in: .whitespacesAndNewlines)
} // Should return a string like "Watch2,4"
解决方法
您永远不会分配缓冲区来接收机器字符串。 更改
var machine = [CChar](repeating: 0,count: size)
到
import React,{ Component } from 'react';
import { Form,Button } from 'react-bootstrap';
import Items from './items'
import Navbar from './navbar'
class Home extends Component {
constructor() {
super();
this.state = {
value: 'enter',condimentCount: 5,menuItems: [
{
"id": 1234,"item_name": 'chow mein',"category_id": 'meal'
},{
"id": 1235,"item_name": '',"category_id": 'meal'
}
]
};
this.handleCategory = this.handleCategory.bind(this);
}
handleCategory = (event,id) => {
console.log('changing meal type',event.target.value)
//this.setState({value: event.target.value});
// 1. Make a shallow copy of the items
let items = [...this.state.menuItems];
// 2. Make a shallow copy of the item you want to mutate
let item = {...items[0]};
// 3. Replace the property you're intested in
item.category_id = event.target.value;
// 4. Put it back into our array. N.B. we *are* mutating the array here,but that's why we made a copy first
items[0] = item;
// 5. Set the state to our new copy
this.setState({menuItems: items});
}
render() {
let menuItems = null;
if (this.state.menuItems) {
menuItems = (
<div>
{
this.state.menuItems.map((item,i) => {
return <div key={i}>
<MenuItem
key={item.id} //IS THIS CORRECT?
item_name={item.item_name}
category_id={item.category_id}
handleCategory={this.handleCategory}
/>
</div>
})
}
</div>
)
}
return (
<div>
<Navbar />
<div>
{Items}
</div>
</div>
);
}
}
export default Home;
你应该很好!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。